Morocco are the second team so far after Egypt to win all three matches of the group stage, following their lone-goal triumph over South Africa in Group D on Monday.
Coach Hervé Renard made several changes to his starting eleven but had to wait until the 90th minute to claim all three points through Mbark Boussoufa.
South Africa showed more determination all through the meeting in a bid to improve the three points earned from their win against Namibia last week.
In the other game of the group played at the 30 June Stadium in Cairo, Ivory Coast crushed Namibia 4-1 to seal their second place.
The Elephants coach Ibrahim Kamara said ahead of the game that he wanted to react following criticisms back home after their 1-0 debacle against Morocco in matchday two.
Max Gradel opened the scoring for the 2015 champions after 39 minutes while captain Serey Dié made it 2-0 by the 58th minute before Joslin Kamatua reduced the deficit after 71 minutes, as tension shifted to the Ivorian camp.
However, Crystal Palace forward Wilfried Zaha and Maxwell Cornet contributed one goal each (84 and 89 minutes) to end the clash at 4-1.
As things stand, Morocco and Ivory Coast are through to the round of 16 while South Africa could likely finish among the best third placed teams. Namibia are out of the competition.