South Africa will have to wait to discover whether they have qualified for the Africa Cup of Nations last 16 after Morocco scored a late winner.
Mbark Boussoufa netted from close range after a 90th-minute free-kick in Cairo.
It earned Morocco a third successive 1-0 win and sealed top spot in Group D.
With Ivory Coast beating Namibia in the other game to finish second in the group, South Africa finished with three points but could still qualify as one of four best third-placed teams.
South Africa went close through Percy Tau but struggled to create chances against a Morocco side that had already qualified for the knockout stage and always looked the more likely to score.
Achraf Hakimi hit South Africa’s bar before Boussoufa found the target in the closing moments after South Africa had failed to deal with a free-kick.
‘South Africa didn’t get close to scoring’
Nick Cavell, BBC World Service in Cairo
Morocco knew they were already through and came out with that kind of attitude. They were happy to control and soak up the ball and attack when the opportunity arose without taking any risks.
South Africa really did struggle to get into the game. They couldn’t get close to scoring.
They now must pray that four teams fail to win tomorrow to have any chance of reaching the last 16. If four teams lose they could still just edge through to the next round.