First-half goals from Hakim Ziyech and Youssef En-Nesyri helped Morocco to a 2-1 win over Canada that saw them secure top spot in Group F and a place in the last 16 of the World Cup for the first time in 36 years.
Morocco were drawn in a tricky group, including Croatia and Belgium, who finished second and third in 2018 respectively, but took four points off the European sides before holding on for victory against Canada in their final group game.
Morocco will now play Spain in Al Rayyan on Tuesday after Luis Enrique's side finished as runners-up in Group E.
- World Cup group tables | Results | Fixtures
- How the teams lined up | Match stats
- Full schedule for Qatar 2022 | World Cup team guides | Download the Sky Sports App
A victory for Croatia over Belgium - who faced off at the same time as Morocco played Canada - could have seen the North Africans drop to second, but it was the Croatians who finished as runners-up after drawing 0-0 with Roberto Martinez's outfit, who crashed out at the group stage.
Canada, playing at their first World Cup since 1986, pulled a goal back shortly before half-time when Nayef Aguerd inadvertently turned Sam Adekugbe's cross into his own net, but ultimately finished bottom of the standings, without a point.
How Morocco claimed victory
Morocco knew a place in the last 16 would be theirs if they avoided defeat to Canada and they were given an early boost on their way to victory by Milan Borjan, when the goalkeeper passed the ball straight to Ziyech, who chipped the ball back over his head and into the empty net after just four minutes.
Morocco then strengthened their grip on a spot in the knockout stages when En-Nesyri ran onto Achraf Hakimi's through ball before lashing a strike past Borjan at his near post.
Canada struggled to play their way through their opponents, with Tajon Buchanan's far-post chance from Cyle Larin's cross their only opening in the first half, but got themselves back into the game through an unlikely source.
Aguerd stuck out a leg as Adekugbe's left-wing cross flashed across the Morocco box, with the West Ham defender's touch diverting the ball past Yassine Bounou at his near post.
En-Nesyri had another goal ruled out for an offside against the luckless Aguerd in first-half stoppage time, but the second half saw Walid Regragui's side take a step back, apparently happy to protect their lead.
Canada seized the initiative, with Ismael Kone impressing from the bench and Atiba Hutchinson seeing a header hit the underside of the bar and bounce onto the goalline in what was likely his final international, but they couldn't find an equaliser and fell to their sixth defeat in six World Cup matches.
What does the result mean?
Morocco's win means they top Group F on seven points, while Canada exit the World Cup with three successive defeats.
Croatia claimed second place with their 0-0 draw against Belgium, whose group-stage exit surely brings the curtain crashing down on their 'Golden Generation'.
The runners-up in Group E, Spain, await Morocco, while the winners, Japan, face Croatia.
Player of the match - Hakim Ziyech
Ziyech has struggled to show his undoubted talent since joining Chelsea in 2020 but it was on full display for Morocco as they saw off Canada.
The winger stayed calm to lift the ball over Borjan and give Morocco the lead when the Canada goalkeeper presented him with possession early in the game and he went on to be at the heart of the North Africans' impressive performance.
Starting on the right of a front three, Ziyech linked well with right-back Hakimi and striker En-Nesyri to show why Regragui was so keen to bring him back from international exile.
Chelsea supporters will be hoping this version of Ziyech returns from Qatar and that he can finally display the quality that convinced them to sign him from Ajax.
What the managers said...
John Herdman, Canada's manager, bemoaned the fact that Hutchinson's header failed to cross the line as the country's search for their first World Cup points continues.
Speaking to the BBC, the Englishman said: "We were two inches away from scoring the first points for Canada. We were willing the ball to cross the line but it didn't.
"We showed Canadian grit and spirit. At times we showed quality. I'm proud of what these lads have showed here. It's going to sting. But we competed at every match.
"There's a quality gap and we're trying to close that. We'll keep learning and we'll be back stronger. This is our first step into the big unknown. We found a lot of things out - we can compete and we were close."
Walid Regragui was in bullish mood after full-time, saying: "We are going to be a very difficult team to beat."
Morocco became the second African side to qualify for the last 16 in Qatar - after Senegal - while Cameroon and Ghana are also still in with a chance of making the knockouts.
"I think African teams can go far," predicted Regragui. "Why shouldn't we dream of winning the World Cup?"
Davies drowned out amid Canadian tinkering
One of Canada's defining characteristics during John Herdman's time in charge has been their ability to adapt to the manager's various tweaks of formation.
When it works, switching systems from game to game can help a side to punch above their weight by nullifying opponents' strengths and exposing their weaknesses. Herdman guided Canada to their first World Cup in 36 years, so it's hard to argue with the Englishman's approach.
However, when the tinkering backfires, it can make a team look muddled - a description that could be levelled at Canada in Qatar.
Their three matches saw them line up in three different formations, while Alphonso Davies - undoubtedly the star of Canadian football - was handed three different starting positions and was regularly shifted around the field as Herdman made further in-game tweaks.
Davies regularly lines up at left-back for his club, Bayern Munich, but the touch map below shows how varied his usage was with Canada at the World Cup.
While it's not uncommon for international sides to use players differently to how they are deployed at club level - Real Madrid defender David Alaba often plays in midfield or on the wing for Austria, for example - Canada didn't seem to know how to get the best from Davies.
The 22-year-old played at wing-back, on both flanks in a 4-4-2 and as part of a front two, but Canada's inability to make him the focal point of their game is shown by the fact he managed just one shot during the tournament.
It's hard to be too harsh on Canada, given they outplayed Belgium in their opening game and took the lead in their second against Croatia.
But the way in which they slumped to defeat against Croatia and then failed to respond to Herdman's changes against Morocco begged the question of how they would have performed with a more settled approach.