South Africa scored two second-half goals on Tuesday night in San Pedro to remove favourites Morocco from the 2023 Africa Cup of Nations after Mali beat Burkina Faso to reach the last eight for the first time in a decade.
Evidence Makgopa opened the scoring in the 57th minute for South Africa at the Stade Laurent Pokou.
The 23-year-old Orlando Pirates striker latched on to Themba Zwane's defence-splitting pass and coolly slotted the ball under the Morocco goalkeeper Yassine Bounou.
The shock spurred Morocco into a response but they could not break down the obdurate South Africa back line.
However, they were offered a chance in the 83rd minute when Mothobi Mvala was penalised for stopping Ayoub El Kaabi’s shot with his arm in the penalty area.
Achraf Hakimi stepped up but his spot kick hit the cross bar and went out for a goal-kick.
As Africa's top ranked team looked for an equaliser, they left themselves open to South African counters and their task was further complicated when midfielder Sofyan Amrabat was sent off in injury-time for a crude lunge on Teboho Mokoena as he tried to break through.
From the resulting free-kick, Mokoena punished Amrabat's belligerence with a sumptuous strike over the Moroccan wall and into the top right hand corner of Bounou's goal.
South Africa will play Cape Verde on Saturday in the quarter-final in Yamoussoukro.
Burkina Faso skipper Bertrand Traoré scored from the resulting penalty to halve the deficit.
Though they pressed for parity, Burkina Faso could not level.
Read more on RFI English
Morocco and East African trio picked to host 2025 and 2027 Cup of Nations
Humility is the key to winning South Africa clash, says Morocco boss Regragui
Mali, Namibia and Burkina Faso open Cup of Nations accounts with victories