Mandla Masango gave Bafana Bafana the lead, but John Boye equalised before the Marseille star popped up to win it and secure a spot in the next phase

Andre Ayew scored a dramatic late winner to see Ghana into the Africa Cup of Nations quarter-finals as Group C winners in a 2-1 victory over South Africa.

Avram Grant’s men looked destined to crash out of the tournament after Mandla Masongo gave South Africa the lead in the 17th minute, but mounted a late fightback and remain in the hunt for the title.

John Boye restored parity 17 minutes from time to give Ghana hope, but they were still on course to be sent home as the clock ticked down.

However, Ayew latched onto the end of a Baba Rahman cross to put the ball into the back of the net in the 83rd minute, and see Ghana into the next round.

Grant’s side finish top thanks to their head-to-head record against Algeria, who beat Senegal 2-0. South Africa are eliminated as the group’s bottom side.

The South African backline was given a stern examination of its capabilities in the early stages.

After Jonathan Mensah had headed just wide from a free-kick in the fifth minute, Eric Mathoho had to block a Jordan Ayew strike on the line, with goalkeeper Nhlanhla Khuzwayo – one of four changes to the side that drew 1-1 with Senegal – having spilled the ball into his path from an earlier effort.

Ghana, who brought Wakaso Mubarak in for Emmanuel Agyemang-Badu in the middle of the park, were having the better of the early possession, but a moment of inspiration handed South Africa the lead out of the blue in the 17th minute.

Having lost his footing when initially attempting to control a long ball forward with his chest, Masangoclimbed to his feet before sending a sublime looping volley over the head of Brimah Razak when a failed headed clearance fell his way.