DURBAN, South Africa (AP) -- Host South Africa twice came from behind to draw with Morocco 2-2 on Sunday and advance to the quarterfinals after finishing top of Group A in the African Cup of Nations.
The result meant that Cape Verde also reached the quarterfinals in its first major tournament after beating Angola 2-1 in a game that took place simultaneously in Port Elizabeth. Morocco finished third in the group.
Goals from Issam El Adoua and substitute Abdelilah Hafidi appeared to have put Morocco through for the first time since 2004.
But on both occasions South Africa fought back, with May Mahlangu and Siyabonga Sangweni ensuring the hosts topped their group on goal difference.
Defender Sangweni was an unlikely hero when he opened the scoring in South Africa's 2-0 win over Angola, and his contribution proved even more crucial here as his 87th-minute goal kept Bafana Bafana in the tournament.
Morocco attacked from the start with Youssef El Arabi testing South Africa goalkeeper Intumeleng Khune in the fourth. Morocco took the lead just six minutes later when El Adoua beat Khune to a corner to nod home.
South Africa continued to struggle in the first half with Khune forced to make vital saves to deny Chahir Belghazouani and Kamel Chafni before the break.
The hosts had a couple of unconvincing penalty appeals, and Bernard Parker's shot flashed wide of Morocco's goal.
South Africa replaced striker Katlego Mphela with Ajax playmaker Thulani Serero early in the second half and Morocco coach Rachid Taoussi looked to shut up shop as he made two defensive substitutions by the 63rd minute.
The decision came back to haunt Taoussi eight minutes later, when Mahlangu found space on the edge of the penalty area to curl his shot into the top-right hand corner.
With Angola leading 1-0 at the time, Morocco needed another goal to progress and they found it when the ball dropped to Hafidi 12 yards from South Africa's goal and the substitute fired through several players.
However, knowing that defeat would mean an exit in the event of a Cape Verde victory, South Africa pushed forward and equalized again when Sangweni mirrored Mahlangu's goal from the edge of the area.
With news of Cape Verde's victory coming through, Morocco threw everything forward in the hope of a late winner, but South Africa held on to leave several Moroccan players in tears.