DURBAN, South Africa -- Host South Africa twice came from behind to draw with Morocco 2-2 on Sunday and advance to the quarterfinals of the African Cup of Nations after finishing top of Group A.
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, who scored in South Africa's 2-0 win over Angola, again made a crucial contribution as his 87th-minute goal kept Bafana Bafana in the tournament.
"If you know there are only a few minutes left I think you throw everything you have forward. He's obviously gone up and taken a chance and it's paid off," South Africa captain Bongani Khumalo said. "It gets tougher now. We're at that sensitive part of the competition and we need to believe in each other and work even harder."
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 as celebrations started in the stands of the Moses Mabhida Stadium.
"It's a great occasion now for the team, for myself and especially for the South African people," South Africa coach Gordon Igesund said. "It was very important for us to qualify (for the second round), hosting this tournament, and I think we've made the country proud.
"I wasn't happy in the first half, because the first goal they scored was a free header, which shouldn't happen at this level. We got a beautiful goal to come back, and then once we were level we went back into defence and they scored their second goal. We had to dig really deep for the result today."