Late leveller sends hosts South Africa through
Defender Siyabonga Sangweni levelled four minutes from time as they edged out massive underdogs Cape Verde Islands for a spot in the last eight.
South Africa would have become the first hosts in 10 editions of the tournament to miss out on the knockout stages if they had lost to Morocco who were eliminated after drawing all three matches.
Cape Verde beat Angola 2-1 in the other Group A match in Port Elizabeth on Sunday.
It was a dramatic conclusion to the section and came as a great relief to South Africa who had gone behind in the 10th minute.
Issam El Adoua headed Morocco in front as the host nation made a feeble effort to clear a corner.
South Africa could have gone 2-0 down a minute later when goalkeeper Itumeleng Khune saved from Chahir Belghazouani.
Khune made another important stop just before half-time when Kamel Chafni broke free and his effort was smothered by the keeper as he came charging outside the penalty area.
Keeper Khune then excelled again as he denied Youssef El Arabi in a one-on-one situation.
South Africa, who managed only one shot in the first half, played with more enterprise after the break with two free kicks from Thuso Phala testing Morocco goalkeeper Nadir Lamyaghri.
May Mahlangu lifted the 50,000 crowd at the Moses Mabhida Stadium when he curled in the equaliser from the edge of the area in the 71st minute.
Morocco went back on the attack, throwing players forward, and were rewarded with eight minutes to go when Abdelilah Hafid swept in a pass from fellow substitute Zakarya Bergdich.
South Africa kept fighting and four minutes later Sangweni found himself clear and he expertly bent the ball into the net.
The hosts now stay in Durban for their quarter-final against the runners-up in Group B who will be determined on Monday.