Canada, who had already been eliminated, fell behind early on thanks to a major error by goalkeeper Milan Borjan to gift Hakim Ziyech the opener.
Youssef En-Nesyri thumped past Borjan to extend their first-half advantage, but Morocco allowed Canada to stay in it when Nayef Aguerd deflected in an own goal before the break.
The Atlas Lions are into the knockout stages for the first time in 36 years, and now face a wait to find out their last-16 opponents, who will be the Group E runners-up.
Morocco needed a win or a draw to reach the last 16, while Canada had already been knocked out following losses to Belgium and Croatia.
Alphonso Davies became the first Canadian to score a goal at a men's World Cup in that second clash, with the 2026 tournament co-hosts hoping to make more history and pick up the first World Cup points in their history with their final match in Qatar.
Their afternoon got off to a catastrophic start when Steven Vitoria mis-hit a backpass and goalkeeper Borjan rushed out of the area.
He initially took possession but then gifted the Atlas Lions the opener when he failed to clear and rolled the ball to Ziyech, who calmly chipped Borjan for the fourth-minute opener.
Canada nearly got on the scoresheet when Cyle Larin took control of the ball inside their half and drove down the right before sending a cross to the back post, but it was a touch too heavy for a sliding Tajon Buchanan to push past Moroccan goalkeeper Yassine Bounou.
Morocco soon extended their lead when Achraf Hakimi fed En-Nesyri, who broke down the right before thumping a low effort past Borjan at the near post, becoming the first Moroccan to score in two different World Cups.
Canada's second goal of the tournament came in unexpected fashion when Sam Adekugbe directed a cross into the area and Aguerd stuck out his right foot to clear but instead directed the ball past a diving Bounou.
En-Nesyri thought he had restored Morocco's lead in stoppage time but Canada were spared by the offside flag.
Canada looked brighter after the break and dealt the North Africans a late scare when 39-year-old substitute Atiba Hutchinson, the oldest outfield player in Qatar, pinged a header off the crossbar.
It fell on the goal-line but did not cross, and a clumsy late save from Bounou ensured Canada's historic first point will not come until their hosting turn at the earliest.