Morocco are through to the last eight of the Africa Cup of Nations after a 2-1 victory over stubborn Malawi at the Ahmadou Ahidjo Stadium in Yaounde.
Gabadinho Mhango put Malawi into an early lead with an exceptional strike.
But Sevilla striker Youssef En-Nesyri levelled at the end of the first half and Paris St-Germain's Achraf Hakimi sealed the win for the Atlas Lions.
Morocco's quarter-final opponents will be either Egypt or Ivory Coast, who meet in Douala on Wednesday.
A well-observed silence was held before kick-off in memory of those killed at Yaounde's Olembe Stadium on Monday night.
The players wore black armbands and messages of condolence were displayed on the advertising hoardings.
Morocco were clear favourites, but it was Malawi who struck first in the seventh minute with Mhango's outstanding effort.
The Orlando Pirates star, who was closer to halfway than to goal, spotted Morocco keeper Yassine Bounou off his line and struck a powerful shot into the top corner.
A back-pedalling Bounou, returning to the line-up after sitting out Morocco's last group game, got a hand to it but could not prevent a memorable goal.
Mhango had an excellent chance to extend the Flames' lead after 24 minutes when he broke clear and shot just wide.
Despite Mhango's efforts, Morocco totally dominated possession.
Sofiane Boufal and En-Nesyri both went close and Wolves defender Romain Saiss struck the bar from close range.
Charles Thomu in the Malawi goal made a series of fine saves, including one to deny Hakimi.
But he wasn't able to keep out En-Nesyri's powerful header in stoppage time at the end of the first period.
Morocco started strongly at the start of the second half but again failed to press home their advantage.
They felt they should have had a penalty when the ball struck the trailing hand of Malawi defender Dennis Chembezi, but it was rejected after a VAR check.
They finally had the lead in the 70th minute when Hakimi struck a brilliant free-kick, curled over the wall and into the near top corner.
It was no more than Morocco deserved and they will feel the margin of victory should have been much wider.
Malawi qualified for the last 16 as one of the best third-placed teams and were playing their first knockout game at a Nations Cup finals.
Despite barely having 20% possession they made sure the North Africans could never relax and tried to hit Morocco on the break until the end.