Morocco had to leave it late to defeat South Africa in their final Group D encounter at the Al Salam Stadium.
A 90th-minute strike from Moubarak Boussoufa for the Atlas Lions broke the heart of the Bafana Bafana after a fiercely contested game.
Meanwhile, the other Group D fixture which was played at the June 30 Stadium saw Ivory Coast beat Namibia 4-1.
Goals from Max Gradel, Serey Die, Wilfred Zaha and Maxwell Cornet ensured the Elephants booked a slot in the round of 16.
Morocco topped the Group D with 7 points and Ivory Coast qualified to round 16 with seven and six points respectively while South Africa who are on three points will hoping to qualify as one of the third best-placed teams should other results go in their favour.