John Boye and Andre Ayew scored as Ghana came from behind to defeat South Africa 2-1 in Mongomo and seal their progress to the quarter-finals as Group C winners.
Both teams were facing elimination from the tournament after Mandla Masango’s fantastic first-half strike, but Boye equalised for Ghana in the 73rd minute.
Ayew’s header 10 minutes later ensured the four-times winners progressed as Group C winners, with Algeria joining them. Bafana Bafana finished bottom of their group with just a solitary point.
Ephraim Mashaba’s men needed to win and hope Algeria lost to Senegal if they were to progress while the Black Stars only needed to defeat South Africa.
South Africa were forced into desperate defending early on when Eric Mathoho brilliantly cleared off the line from Jordan Ayew, whose strike was parried back into his path by goalkeeper Nhlanhla Khuzwayo.
Ghana’s profligacy came back to haunt them as Mashaba’s men took the lead in sensational style in the 17th minute.
There appeared to be little danger when Masango fell over, but when the ball was headed back into his path, the Kaizer Chiefs forward was up quickly to send a volley over Brimah Razak.
It raised South Africa’s hopes of progression but Ghana responded well, with Daniel Amartey missing with a header and Khuzwayo pulling off another excellent save from Baba Rahman’s strike.
South Africa, though, remained a threat going forward and Razak kept the deficit to one at the interval with a fine stop from Bongani Ndulula’s downward header.
Asamoah Gyan’s free-kick went just over while Jordan Ayew’s effort had Khuzwayo struggling to tip round the post, but otherwise Ghana struggled going forward.
Both sides were facing the prospect of going out, with Algeria’s lead over Senegal thwarting South Africa’s hopes, but Ghana gave themselves a lifeline in the 73rd minute.
Andre Ayew’s cross-cum-shot fell into the path of Boye, who turned and slotted home to level matters.
The goal galvanised Ghana and they took the lead seven minutes from time, with Andre Ayew reaching Rahman’s left-wing cross before Khuzwayo to head into an empty net.
Riyad Mahrez and Tottenham midfielder Nabil Bentaleb were on the scoresheet as Algeria sealed their place in the quarter-finals with a 2-0 win over Senegal in Malabo.
It is the first time since 2004 that Algeria have reached the last eight of the competition, but there was heartbreak for Senegal as they were sent home when Ghana beat South Africa.
Knowing only a victory would guarantee them qualification, Algeria were quickly out of the blocks and should have taken the lead inside the opening two minutes following some terrible Senegal defending.
Papy Djilobodji’s mis-control gifted Sofiane Feghouli a free run on goal but the defender’s blushes were saved by his keeper Bouna Coundoul, who denied Feghouli as the Almeria forward attempted to round him.
Algeria did not have to wait long to take the lead, though, and again the standard of Senegal defending was poor.
Faouzi Ghoulam launched a long diagonal free-kick from the halfway line over the Senegal defence and Mahrez, having been allowed to run in behind completely unchecked, neatly brought the ball down and slotted it under Coundoul.
Algeria knew they needed a second goal to ease any concerns, and they very nearly got it in the 75th minute when Coundoul pushed out a cross to Saphir Taider inside the area.
However, Taider’s shot was blocked on the line by Kara Mbodji and Senegal also escaped from the rebound.
Algeria would not be denied, though, and eight minutes from time they did get the crucial second goal.
Feghouli picked up a loose ball before laying it across the edge of the area to Bentaleb, who took a touch before lashing a left-footed drive past Coundoul.