Morocco defeated South Africa 1-0 in their final 2019 Africa Cup of Nations (AFCON) Group D match at the Al Salam Stadium in Egypt on Monday evening.
The result saw the Atlas Lions progress to the knockout stages as group winners, while Bafana Bafana finished third in the group.
Bafana Bafana and the Atlas Lions both struggled to create clear-cut chances in the opening stages of the encounter.
Morocco were without their captain Medhi Benatia, who had been rested, but Romain Saiss looked solid at the heart of the team's defence.
The England-based defender made a crucial interception in the box in the 14th minute following a good move involving by South Africa duo Thamsanqa Mkhize and Thulani Serero.
Both teams grew in confidence as the match progressed and Percy Tau's shot from outside the box narrowly missed the target just after the half-hour mark.
Almost immediately at the other end, Youssef En-Nesyri forced South Africa goalkeeper Ronwen Williams into a low save with a powerful shot.
The two teams then pushed for the opening goal, but they could not find the back of the net in the first-half and the score was 0-0 at half-time.
South Africa threatened first after the restart as Mkhize's powerful low shot went inches wide of the target in the 48th minute after he was set-up by Themba Zwane.
Morocco also came very close to scoring when Achraf Hakimi cut inside and he unleashed a thunderous effort which hit the crossbar with Williams well-beaten.
The Atlas Lions were the better side in the closing stages of the game and Wiliams had to make a good save to deny En-Nesyri with 15 minutes left.
Morocco's pressure paid off as Mbark Boussoufa pounced inside the box and he fired past Williams in the 90th minute to hand the Atlas Lions a 1-0 win over South Africa.