The defending champion was joined in the knockout stage by Rwanda, which trailed Togo twice before Jacques Tuyisenge and Ernest Sugira scored within six minutes to create a 3-2 victory in Limbe.
Morocco, which hopes to become the first back-to-back winner of the competition for home-based players, topped Group C with seven points, Rwanda had five, Togo three and Uganda one.
In the quarter-finals, Morocco takes the Group D runner-up and Rwanda the Group D winner, and their rivals will be known after Guinea faces Tanzania and Zambia meets out-of-contention Namibia.
Record two-time champion Democratic Republic of Congo meets host nation Cameroon and Mali tackles Congo Brazzaville in the other last-eight pairings.
One Nations Championship record was equalled and another two broken as Morocco flexed its muscles to overwhelm Uganda at Stade Reunification in the Cameroon economic capital.
Morocco became only the second nation after Tunisia in 2016 - against Niger - to score five goals in a match at the tournament.
The five goals lifted the overall Moroccan total to 33 from 16 matches, bettering the 31 of DR Congo.
As if being hammered and eliminated was not bad enough, Uganda has conceded 24 goals during five appearances in the Nations Championship, more than any other country.
A night that ended so badly for the Ugandans began promisingly with an Ibrahim Orit goal on 25 minutes not only giving them the lead, but also lifting them to second in the table, ahead of Morocco.
That situation remained until the fourth minute of stoppage time at the end of the first half when Morocco captain Ayoub el Kaabi converted a penalty awarded when Ugandan Mustafa Mujuzi handled.
Rahimi stole the second-half show, scoring either side of a goal from Hamza el Moussaoui, to give the north Africans a 4-1 lead.
The goal deluge continued in the closing minutes as Saidi Kyeyune pulled one back for Uganda, whose goalkeeper, Charles Lukwago, then turned an Abdelilah Hafidi shot into his own net.
In a Limbe thriller, Yendoutie Nano gave the Togolese a lead that was cancelled by Olivier Niyonzima in the final minute of the first half.
Bilal Akoro put Togo, the only nation among the 16 in Cameroon competing at the Nations Championship for the first time, ahead a second time before Rwanda struck twice to finish runner-up.