After beginning their World Cup 2022 campaign with a goalless draw against Croatia, victories over Belgium and Canada ensure Morocco finish top of Group F.
Hakim Ziyech opened the scoring in the fourth minute before Youssef En-Nesyri doubled the advantage in what proved to be an entertaining first period.
Canada managed to halve the deficit thanks to Nayef Aguerd's own goal, while En-Nesyri had an effort chalked off in first half stoppage time.
John Herdman's side went close to finding an equaliser in the second period when Atiba Hutchinson's powerful header hit the crossbar before dropping down onto the goalline, but Morocco held firm to secure a narrow victory.
While Canada exits the tournament without picking up a point, Morocco will face the team that finishes second in Group E on Tuesday.
Morocco went into the contest knowing that a draw would be enough to secure their passage to the last 16, but their sights were firmly set on a victory after they were gifted an opening goal in the fourth minute.
Steven Vitoria's under-hit backpass put Milan Borjan under pressure, but rather than clear his lines, the goalkeeper gifted possession straight to Ziyech, who produced a sublime first-time chip over Borjan to spark scenes of jubilation among the large Moroccan contingent in the Al Thumama Stadium.
After falling behind, Canada showed signs of a response when Cyle Larin sent a low cross to the far post, but a sliding Tajon Buchanan could not make any contact with the ball.
While Canada had already been eliminated prior to the final group game, their hopes of picking up their first point of the tournament were dented when Morocco doubled their lead in the 23rd minute.
Achraf Hakimi found En-Nesyri with an accurate ball played behind the Canadian defence, and the Sevilla striker remained composed to fire a low strike past Borjan.
Despite being second best for the majority of the first half, Herdman's side managed to pull a goal in the 40th minute when Nayef Aguerd accidentally directed a cross past his own goalkeeper.
In first-half stoppage time, En-Nesyri thought he had restored Morocco's two-goal advantage, but his volley was ruled out for offside against a player who had interfered with the goalkeepers' line of vision.
In contrast to the opening 45, Canada dominated possession in the second half as Morocco took the decision to protect their lead.
The best chance of the second period arrived in the 72nd minute when Atiba Hutchinson went agonisingly close to finding an equaliser.
Hutchinson rose highest in the box to meet Junior Hoilett's corner, and his powerful header thumped the underside of the crossbar before hitting the goalline, while Alistair Johnston could only nod the subsequent rebound over Bono's goal.
As the contest entered the closing stages, Morocco began to drop deeper in a bid to protect what they had, and with Canada ultimately unable to find a late leveller, Walid Regragui and his side could celebrate a memorable victory that helped clinch top spot in the group.