Siyabonga Sangweni equalised for Bafana Bafana with less than five minutes left in Durban on Sunday as the Africa Cup of Nations hosts booked their place in the tournament quarterfinals for the first time in more than a decade.
Issam El Adoua fired the North Africans in front after a 10th minute Itumeleng Khune error, before May Mahlangu equalised with an exquisitely curled effort into the top corner with less than 20 minutes remaining.
Mehdi Namli then restored Morocco's advantage in the 82nd minute, before Orlando Pirates defender Sangweni netted his second goal of the tournament, bending in to the far post, to clinch a share of the points.
Khune also turned from villain to hero as the game progressed, twice saving one-on-one chances from the Atlas Lions.
The result and single point turned out to be a vital one as Cape Verde Islands beat Angola 2-1 in a simultaneous pool match and a defeat would have but have put Gordon Igesund's men out of the tournament.
But they showed incredible fighting spirit in front of a capacity 50 000 crowd at the Moses Mabhida Stadium and will now remain in Durban for their last eight encounter against the Group B runners-up.
The Moroccans, desperate for victory, made a fast start to the game as Youssef Kaddioui and Youssef El-Arabi came close, the latter's shot forcing keeper Khune to save at his feet.
At the other end, Tokelo Rantie, a player that passed a late fitness test, had a strong penalty shout against defender Adil Hermach, but Gambian referee Bakary Gassama waved play on.
Meanwhile, Morocco continued to venture forward with intent and it took a last-ditch challenge from Dean Furman to deny Abdelaziz Barrada.
From the ensuing corner on the left, the same visiting midfielder delivered to the centre of the penalty area where El Adoua headed in after Khune recklessly raced off his line.
The Kaizer Chiefs captain was again almost caught out in the 20th minute, but somehow managed to dig the ball out after Chahir Belghazouani shot from the left with Rachid Taoussi's men completely dominating.
The noisy home crowd were trying their best to get South Africa going and the forwards finally responded in the 27th minute when Mahlangu and Thuso Phala combined to play in Bernard Parker, but the striker flashed his shot across the face of goal.
Rantie then showed a good burst of speed as he raced away down the left, but as with previous Bafana Bafana attacks, decision making in the final third was poor as another potential opportunity went begging.
In the final minute of the half, Khune did well to make up for his earlier error when he raced off his line to keep out Kamel Chafni, who was one-on-one with the goalkeeper, to keep it 1-0 at the break.
After the restart, Morocco goalkeeper Nadir Lamyaghri was finally forced into a first real save in the 53rd minute when Phala's free-kick from the left bent toward the left corner, only for the ball to be scrambled away for a corner.
An error from Sangweni then allowed El-Arabi a clear one-on-one chance, but Khune stuck out his arm at the last minute to keep out the striker.
Phala again illustrated his threat with set-pieces when he forced the Moroccan custodian to dissect wide with a dipping free-kick that skidded off the wet surface.
But Lamyaghri could do nothing about South Africa's equaliser after Mahlangu was set up by the slick Rantie.
The crowd celebrated the moment, but they were stunned into silence once again when the Atlas Lions retook the lead via Namli, who controlled with his chest inside the box and fired low past Khune.
With three minutes to go, Sangweni ventured forward and showed his instinct for goal when he opted to shoot instead of pass, curling low past Lamyaghri from the edge of the area.
That proved the final chance as the hosts burst into a frenzy on the full-time whistle.
South Africa, who last reached the Afcon quarterfinals at the 2002 tournament in Mali, finished top of group A. They will face the group B runners-up at Moses Mabhida Stadium on Saturday.
Debutants Cape Verde Islands finished second in the group. They will play the group B winners in Port Elizabeth in the next round.
Morocco and Angola were eliminated from the competition. – Sapa