Canada started the brighter of the two sides and carved out a string of first-half chances without finding the breakthrough.
Morocco struck against the run of play to take a 1-0 lead, a blow that Canada were unable to fully recover from despite continuing to press for an equaliser.
The North Africans doubled their advantage in the second half, punishing Canada on the counter-attack after the Canadians had committed men forward in search of a way back into the tie.
Canada coach Jesse Marsch was proud of his players despite the result.
"They made a couple more plays than us but cranking up the intensity was not the issue," said Marsch. "It's just they have a little bit more quality in the final third and we lacked a bit of ability to make a play when we needed it."
"But in terms of the match plan and the idea of how we want to play football and the idea of a bunch of guys believing in themselves and going after it and taking on a top team that hasn't lost for I don't even know how long and taking them to the limit, we were the much better team in the first half and even the beginning of the second half."
"There was one play that made it 1-0. Across the pitch we had 11 incredible performance in the first half. We were unlucky not to get the lead. Then it's the fine details. What a privilege our fans have had to root a team on like this that goes after the game."
Midfielder Stephen Eustaquio echoed his manager's sentiment.
"I think we did an amazing tournament," said Eustaquio. "Until the half-time of this game we were outstanding. Everybody back home should be very proud."
"We felt we could have won this game especially in the first half. We were on top of them, we were fighting, we were creating chances, our set-pieces were on point. But from a set-piece we let that slip and then obviously we want to go after it and we got hurt on transitions."
Morocco coach Mohamed Ouahbi admitted his side had to dig deep to see off a determined Canada.
"We are very happy," said Ouahbi. "It's a World Cup match and these are difficult games with teams playing for their lives. We reacted very well in the second half in the second balls and the duels."
"I have to recognise that Canada were impressive โ they played a top match. It was no surprise for us but in the second half we were able to profit from the space they left us โ that was the key."
Morocco midfielder Azzedine Ounahi, speaking to beIN Sports, scored on the day and reflected on a hard-fought victory.
"I'm really happy to have scored today," said Ounahi. "It wasn't easy for us. We saw that Canada caused us problems, especially in the first half."
"This match will help us grow. We've seen that no game is easy in this competition, but the most important thing is that we're through."
Morocco now turn their attention to the next round, while Canada's World Cup journey ends with a performance that will give them plenty of encouragement for the future.
