African Cup of Nations hosts South Africa ensured their progression to the quarter-finals as Group A winners as Siyabonga Sangweni salvaged a late 2-2 draw with Morocco.
Rachid Taoussi's Morocco crashed out, though, with tournament minnows Cape Verde taking their place in the last eight courtesy of an historic 2-1 win over Angola.
That result meant South Africa would have qualified regardless but Bafana Bafana struggled to respond to Issam El Adoua's opener.
May Mahlungu finally made the Durban crowd sit up and take notice with a fine finish after the break but Abdelilah Hafidi's quick response inspired fear on the South African bench.
Sangweni secured the point needed to tee up a preferable tie in the next round and news of Cape Verde's late winner did not appear to reach the Morocco team in time for them to mount one last raid.
Morocco would dominate the first half and Abdelaziz Barrada had South Africa goalkeeper Itumeleng Khune sprawling within four minutes.
The opening goal arrived in the 10th minute as El Adoua rose highest to head home from a corner, with the Bafana Bafana defence in disarray.
Youssef El Arabi fancied scoring a second goal in the tournament, having netted against Cape Verde, but Bongani Khumalo got in a crucial block on the Granada man's left-footed effort.
Khune did well to tip a low drive from Chahir Belghazouani around a post in the 20th minute and it suddenly seemed that Morocco had run out of ideas.
Bernard Parker finally gave the home faithful something to cheer about with an effort from the edge of the area but his accuracy was lacking.
South Africa made a much brighter start following the break, though, with Thuso Phala forcing a fingertip save from Nadir Lamyaghri with his curling free-kick.
Khune emerged victorious from a one-on-one with El Arabi on the hour mark and soon Bafana Bafana were in the ascendancy.
Gordon Igesund's men continued to make up ground and when the equaliser came in the 71st minute, it was spectacular.
Mahlungu was unchecked on the edge of the Morocco box, his sumptuous curling finish sending the Durban crowd into raptures.
Hush descended, though, when substitute Hafidi chested down Zakarya Bergdich's cross to control and slotted home eight minutes from time.
Sangweni might be a centre-back but he registered his second goal of the tournament in scoring the equaliser.
When the Orlando Pirates man found himself on the edge of the box with four minutes to go, he twisted his body to clip the ball across Lamyaghri and into the bottom corner.