Morocco did host nation Ivory Coast a colossal favour by beating Zambia to send the Elephants into the Africa Cup of Nations last 16 by the back door.
Zambia needed a point from the Group F finale to deny the hosts the fourth and final qualification spot for the group stage’s best third-placed teams.
But Hakim Ziyech slammed in from close range to send Zambia home, Ivory Coast through and help Morocco top the group.
The Atlas Lions will face South Africa in the knockout stages on Tuesday.
Meanwhile, Ivory Coast, who sacked boss Jean-Louis Gasset earlier on Wednesday after their disappointing group displays put the West Africans on the brink of an early exit, will meet Senegal in a mouth-watering last-16 tie on Monday.
For that, the Elephants owe a debt of gratitude to Walid Regragui’s side for handing them a reprieve at the eleventh hour, ensuring another dramatic twist to end an enthralling, unpredictable set of group games.
Head coach Regragui was absent from the Morocco dugout against Zambia after being given a four-match ban by the Confederation of African Football for “post-match incidents” following Sunday’s 1-1 draw with DR Congo.
Two games of his ban have been suspended for a year but the 48-year-old will also have to watch the last-16 meeting with Bafana Bafana from the stands, unless a Moroccan appeal succeeds.
Relief for host nation as Morocco help out
After Ivory Coast were thrashed 4-0 in a stunning Group A upset by Equatorial Guinea on Monday, they had the fall-back of knowing that only two third-placed teams across the six groups would be eliminated.
When Ghana conceded twice in stoppage-time to draw with Mozambique later that day and finish Group B with only two points, the Elephants’ spirits were quickly raised, needing only to better the record of one other third-placed side.
But they could only watch on with growing dread as Guinea, Mauritania and Namibia subsequently wrapped up three of the four spots, meaning the Elephants needed Zambia to lose – or they would join fellow big guns Algeria and Ghana in making an embarrassing early exit.
With Morocco already assured of progression after collecting four points from their first two games, the host nation might have worried how seriously the North Africans would take their final group encounter.
But despite making four changes, the Atlas Lions dominated the first half at Stade Laurent Pokou in San Pedro with Nayef Aguerd heading a glorious chance wide before Morocco finally, and deservedly, broke the deadlock.
The goal was harsh on Zambia goalkeeper Lawrence Mulenga who superbly denied Ayoub El Kaabi’s close-range attempt to prod in Achraf Hakimi’s cross, only for Ziyech to rifle home the rebound.
Mulenga kept the hopes of Avram Grant’s side alive in the second half with a smart stop from Amine Adli and an excellent late double save to foil Amir Richardson and Tarik Tissoudali.
But Zambia were restricted to shooting from distance to try to rescue the point they needed as Lameck Banda curled just wide from 20 yards and Emmanuel Banda’s shot from further out was tipped over by Bono.
The Chipolopolo had drawn eight of their previous nine Afcon games but were unable to do so when they needed to most – and they have still not won a match at the finals since being crowned surprise champions in 2012.