Africa’s top-ranked side were hoping to follow up their remarkable run to the last four in Qatar just over a year ago by winning their second AFCON title, 48 years after their first.
Instead they go the same way as Senegal, Tunisia, Algeria and Egypt in being knocked out, meaning none of the five top-ranked African nations will be present in the quarter-finals, as well as none of the four semi-finalists from the last edition.
“We are very disappointed because we came here with the ambition of winning,” admitted Morocco coach Walid Regragui.
“Going out so early was not in our plans but this competition is very difficult.
“I take responsibility for everything that has happened. I never hide. Today I have failed,” he added.
Mokoena stars
A South Africa side featuring eight players from the country’s leading club Mamelodi Sundowns in their starting line-up took the lead in the 57th minute.
Themba Zwane released Orlando Pirates striker Makgopa, who finished past Morocco goalkeeper Yassine Bounou.
Yet a Morocco team missing the injured Hakim Ziyech and Sofiane Boufal were given a glorious chance to equalise late on.
They appealed loudly for a penalty when an Ayoub El Kaabi shot from point-blank range smashed into the arm of South African defender Mothobi Mvala.
The Sudanese referee initially waved play on before awarding the penalty after being called across to review the incident.
Up stepped Hakimi, the runner-up to Nigeria’s Victor Osimhen for the most recent African footballer of the year prize, but he failed to convert from the spot.
Morocco were then reduced to 10 men in the second minute of injury time as Amrabat of Manchester United was dismissed for a last-man challenge on Mokoena as the South Africa midfielder charged towards the penalty box.
Amrabat was initially given a second yellow, a punishment that was upgraded to a straight red following a review.
The excellent Mokoena, one of the Sundowns contingent, then converted the resulting free-kick to finish off Morocco once and for all.