The pair finished level on six points with Mozambique, with the teams having to be separated by goal-difference, and the South Africa coming out on top to head the pool, with Zambia second.
That is because the calculations were made by removing the results against Madagascar. That left all three sides on three points, but South Africa with a +1 goal-difference, Zambia even and Mozambique -1.
Zambia had been expected to see off the Mozambican challenge, but lost 1-0 thanks to a goal from Geny Catamo two minutes before halftime.
It was a surprise defeat for the Zambians, who have looked in complete command through the tournament so far.
South Africa were then given a real scare by Madagascar, but roared back to win the game 5-3 and book their semi-final spot.
They took the lead through Olefile Ramatlo, but three goals in the final 10 minutes before halftime turned the match in the Malagasy’s favour.
Dillan Fanomezantsoa, Stéphan Rakotovao and Mamitina Andriamahazaka netted to turn the tie in the islanders favour, though in among there was also a strike from South Africa’s Azola Tshobeni.
Madagascar led 3-2 at the break, but South Africa managed to turn around the see-saw battle again in the second period when Tshobeni grabbed his second to equalise, and then Alex da Silva and Mohammed Shahabodien added further goals in the final 10 minutes.
That last goal proved crucial for South Africa in lifting them to the top of the pool and they will now face the second-placed team in Group A, which will be concluded on Wednesday.
As things stand Malawi head the pool on goal-difference from hosts Mauritius, with both teams having already booked their place in the semifinals.
They meet in their final pool game on Wednesday at the Stade St Francois Xavier in Port Louis, while Botswana and Zimbabwe play for third place at the MFA Technical Centre.
Both matches kick-off at 15h00 local time (11h00 GMT).
TUESDAY’S GROUP B RESULTS
Mozambique 1 (Catamo 43’) Zambia 0
Madagascar 3 (Dillan 37’, Stéphan 39’, Mamitina 44’) South Africa 5 (Ramatlo 14’, Tshobeni 39’, 72’, Da Silva 81’, Shahabodien 88’)
WEDNESDAY’S GROUP A FIXTURES
Zimbabwe vs Botswana – MFA Technical Centre
Malawi vs Mauritius – Stade St Francois Xavier
Download our app and read this and other great stories on the move. Available for Android and iOS.