Youssef En Nesyri doubled the lead with his right-footed goal from the penalty area in the 23rd minute.
In the 40th minute, Canada narrowed the gap to one with an own goal from Nayef Aguerd.
After five minutes, En Nesyri scored again for his national team but it was disallowed for a tight offside call.
Neither of the teams was able to produce a goal in the second half as the game ended with a 2-1 score in favor of the Atlas Lions.
Collecting seven points, Morocco advanced to the knockout stage for the first time since 1986.
Croatia, who also qualified for the next stage, are second with five points and Belgium are in the third spot with four points.
Canada finished bottom of the group without a point.
Source: Anadolu Agency