The match got off to a blistering start with two goals inside the opening 10 minutes, Josko Gvardiol giving Croatia a seventh-minute lead before Achraf Dari levelled things up again just two minutes later.
However, the decisive moment came shortly before half time when Mislav Orsic struck the winner in style, curling a fine effort over the keeper and in off the inside of the far post.
The result condemns Morocco to a fourth-placed finish in Qatar, although they have still produced the best-ever performance by an African nation at a World Cup.
Croatia, meanwhile, match their finish from the 1998 World Cup and have now finished inside the top three in half of the World Cups they have ever participated in.
HIGHLIGHTS
3rd min: Almost a calamitous start for Morocco as goalkeeper Bono tries to pass the ball across the face of his own six-yard box but slices it, sending it behind for a corner on the other side of the goal. That could have easily gone in for what would have been one of the most bizarre own goals in World Cup history.
7th min: Croatia do have their early breakthrough, and it is one of their standout players Josko Gvardiol who gets it. Croatia work a free kick brilliantly as the ball is clipped to Ivan Perisic, who nods it back across goal for Gvardiol to meet with a fine diving header.
9th min: Immediate response from Morocco as, seconds after scoring at the other end, Gvardiol concedes a free kick which Lovro Majer cannot cut out in the wall, instead sending it looping into the box where Achraf Dari is waiting to nod home from close range.
18th min: Kramaric has a sight of goal as he meets a cross from the left flank with a firm header, but it is straight at the keeper.
24th min: Good work from Luka Modric as he works a yard of space on his left foot outside the box and fires a low strike towards goal which Bono spills, but the keeper recovers in time to push it away from the Croatian attackers following in.
42nd min: Croatia regain the lead just before half time, and it is a beautiful goal from Mislav Orsic as he bends a first-time effort over Bono and in off the inside of the far post. Morocco gave the ball away right on the edge of their own box, but take nothing away from that finish.
47th min: Orsic finds a bit of space and fires a shot towards goal which takes a deflection before rippling the side-netting.
73rd min: Croatia want a penalty as Gvardiol goes down dramatically under the challenge of Sofyan Amrabat, but the referee waves it away. The replay shows that he was caught, but VAR upholds the original decision.
75th min: Big chance for Morocco to level things up 15 minutes from time as Gvardiol fails to clear his lines and Youssef En-Nesyri finds himself with only the keeper to beat, but Dominik Livakovic is straight out to smother him with a big save from point-blank range.
87th min: Mateo Kovacic has the chance to put the game to bed as he beats his man with some dancing feet inside the area, but then drags a weak left-footed effort well wide.
90+6 min: En-Nesyri is so close to forcing extra time in the final minute of stoppage time. For once, he climbs above Perisic to meet a cross with a great header, but it flies just over the bar and bounces back off the roof of the net.
MAN OF THE MATCH - IVAN PERISIC
There were plenty of players on the pitch who have had wonderful World Cups, but there was not really a clear standout man of the match from today's encounter - understandably given this is the game no-one wants to be involved in.
However, Ivan Perisic was perhaps the best of the lot, setting up the opening goal with an inch-perfect cushioned header into a dangerous area and looking both solid in defence and threatening in attack throughout.
The Tottenham Hotspur man may have felt slightly irked to be named at left-back when he knew that one goal would see him become Croatia's outright all-time leading scorer at World Cups, but as always he carried out his duties diligently and showcased his incredible versatility.
STANDOUT MOMENT
There is always something very satisfying about a good old-fashioned diving header, and seeing one in the penultimate game of a World Cup was a joy.
Gvardiol's effort had the added benefit of coming at the end of a lovely free kick routine too, with the ball being clipped over the wall for Perisic, who nodded it back across for Gvardiol to convert.
The masked defender still had plenty to do, but every part of the routine was executed perfectly for one of the more aesthetically-pleasing goals of the World Cup.
A mention should also go to Orsic's second for Croatia, which was probably a better goal overall, but a good diving header is always hard to beat.
BEST STATS
MATCH STATS
Possession: Croatia 51%-49% Morocco
Shots: Croatia 12-9 Morocco
Shots on target: Croatia 4-2 Morocco
Corners: Croatia 6-3 Morocco
Fouls: Croatia 13-11 Morocco
LINEUPS
CROATIA (4-2-3-1): Livakovic; Stanisic, Sutalo, Gvardiol, Perisic; Modric, Kovacic; Majer (Pasalic 66'), Kramaric (Vlasic 61'), Orsic (Jakic 95'); Livaja (Petkovic 66')
MOROCCO (4-3-3): Bounou; Hakimi, El Yamiq (Amallah 67'), Dari (Benoun 64'), Attiat-Allah; El Khannouss (Ounahi 56'), Amrabat, Sabiri (Chair 46'); Ziyech, En-Nesyri, Boufal (Zaroury 64')