Achraf Hakimi scored a brilliant free kick to send Morocco through to the quarter-finals of the Africa Cup of Nations after a 2-1 win over Malawi on Tuesday.
Gabadinho Mhango gave outsiders Malawi, playing their first knock-out game at the tournament, a shock early lead with a stunning goal from 40 metres at Ahmadou Ahidjo stadium.
But Youssef En-Nesyri headed Morocco level in first-half stoppage time before Hakimi drilled in the winner on 70 minutes to set up a meeting with Egypt or Ivory Coast in the last eight.
"When you win you get respect," said Morocco coach Vahid Halilhodzic.
"Malawi scored a fantastic goal. I think we played really well in the first half, unfortunately we lacked efficiency. We had around 10 chances you can't miss.
"But I was always confident. The team was upset but I knew at some point we would score a second goal. We controlled the match and we more than deserve to qualify."
Morocco's quarter-final tie was due to take place at Olembe Stadium in Yaounde but will be moved after a fatal crush outside the ground on Monday, which killed eight people and injured 38.
As with the earlier match between Senegal and Cape Verde in Bafoussam, a minute's silence was observed before kick-off and players of both teams wore black armbands.
Morocco, bidding for a second Cup of Nations title and first since 1976, fell behind on seven minutes when Mhango caught Yassine Bounou off his line with a superb long-range strike.
The Atlas Lions responded by creating a series of chances as Imran Louza stung the palms of Charles Thomu, who then did well to keep out Hakimi's powerful free-kick and the follow-up from Louza.
Mhango dragged wide for Malawi after a terrific long ball from Micium Mhone, but the Flames found themselves under sustained pressure as Morocco captain Romain Saiss volleyed against the bar.
En-Nesyri prodded weakly at Thomu from close range before the Malawi goalkeeper tipped Hakimi's low drive on to the post and then denied Sofiane Boufal with his legs.
Morocco were finally rewarded for their persistence on the stroke of half-time as En-Nesyri rose to power Selim Amallah's deep cross past Thomu.
Hakimi appealed for a penalty in the second half when his cross struck the arm of Malawi defender Dennis Chembezi, but the Paris Saint-Germain star took matters into his own hands to secure victory.
His late equaliser against Gabon had secured top spot in the group stage, and Hakimi delivered again with another magnificent free-kick 20 minutes from time to end Malawi's impressive run.