Hosts South Africa finished top of Group A after their dramatic 2-2 draw with Morocco in Durban knocked the North Africans out of the tournament.
Knowing a win would take them through, Morocco twice led - only to exit the finals, as Cape Verde beat Angola 2-1.
After Issam El Adoua headed home 10 minutes in, May Mahlangu curled home in fine style after 71 minutes.
Mehdi Namli put Morocco in front again but Siyabonga Sangweni broke North African hearts with a late equaliser.
The goal ensured that South Africa, who reached the Nations Cup knock-out stages for the first time since 2002, stay in Durban for Saturday's quarter-final against the Group B runner-up.
It also meant unbeaten Morocco suffered an early exit for the second finals in a row - their departure made all the more painful given that they were just four minutes away from qualifying as group winners.
On a night of unrelenting drama, the Group A climax was full of twists and turns - with the most severe coming when Cape Verde scored a 90th-minute winner to squeeze into the knock-out positions.
It was cruel on an Atlas Lions side who were much improved from their opening displays but who were undone by two high-quality South African goals.
Making light of a partisan packed house in Durban, the North Africans got off to a flier - with striker Youssef El Arabi denied after just four minutes by South African goalkeeper Itumeleng Khune.
Moments later, the 1976 champions opened the scoring as Khune misread a Moroccan corner - allowing centre-back Issam El Adoua to head home from close range.
With a pace to the game seldom seen at these finals, the hosts - who were roared on whenever they advanced - first threatened as the lively Thuso Phala teed up Bernard Parker who dragged his strike wide.
Some South Africans could be seen praying in the stands and they were certainly thankful after Khune raced 20 yards out of his area to break up a Moroccan counter-attack after Kamel Chafni had broken free.
If the first half was a tasty appetiser, the second half delivered a feast of football theatre as all four teams in the group clung on to their hopes of qualification until the dying moments.
After 53 minutes, Phala - whose set-piece delivery was superb all evening - thought he had drawn the 1996 champions level with a free-kick that was flying into the top corner until veteran Moroccan keeper Nadir Lamyaghri pulled off a fine save.
El Arabi then had the chance to put Morocco in control of their destiny when a Sangweni error allowed him a free run at goal from 40 yards - but Khune threw up a hand to make a decisive save from the attempted chip.
With time rapidly ticking away, South Africa levelled as midfielder Mahlangu played a one-two with Tokelo Rantie before bending one of the goals of the tournament into the top corner from the edge of the box - leaving Lamyaghri, 36, with no chance.
At this point, with both games level at 1-1, Morocco were exiting the tournament on the number of yellow cards they had picked up - so their goal after 82 minutes sparked wild celebrations.
Substitute Abdelilah Hafidi controlled a cutback on his chest in the area before firing through the legs of Sangweni and past Khune - prompting Morocco coach Rachid Taoussi to sink to his knees and kiss the turf.
His side were top of the group but their descent started once Sangweni - nominally a centre-back - pitched up on the edge of the penalty area to curl home another fine Bafana Bafana goal.
As the Moses Mabhida Stadium led the celebrations across South Africa, news filtered through that Cape Verde had taken the lead against Angola - leaving some Moroccan players in tears as they struggled to understand the nature of their elimination.
South Africa coach Gordon Igesund:
"We feel very, very proud to have achieved our first objective. I must thank the players and the fans.
"Morocco were a very tough team. I do not want to point out mistakes, but we conceded two goals we should not have done. There are now two games to the final and I believe in my players."
Morocco coach Rachid Taoussi:
"I am very disappointed with the result, but we fought with dignity and honour. I am satisfied. We did not lose a game and the tournament was a positive one for me.
"This Cup of Nations was a step forward and we will now prepare for the 2014 World Cup qualifiers. Our main objective is the 2015 Cup of Nations, which Morocco host."