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.