Morocco have confirmed their progress to the World Cup last 32 but twice had to come from behind to get the better of plucky Haiti with a 4-2 win in an action-packed game on Wednesday, and fell short of topping Brazil in the Group C standings.
Morocco finished level on seven points with the five-times world champions, who beat Scotland 3-0, but are runners-up on goal difference and will play the Group F winners, Netherlands, Japan or Sweden, on Monday in the first knockout round.
Haiti, the first side eliminated from the 48-team tournament after losing their second group game, went ahead early thanks to an own goal, but African Footballer of the Year Achraf Hakimi equalised in the 39th minute.
Wilson Isidor’s thunderbolt restored Haiti’s lead in the 43rd, but the match was soon level again as Ismael Saibari scored his third goal in as many World Cup appearances.
It took until the 78th minute for Morocco to take the lead when substitute Soufiane Rahimi thrashed the ball home from close range after Haiti failed to clear a corner and 20-year-old Gessime Yassine made it 4-2 in the last minute.
"It was a crazy game, too many goals but we are really delighted to qualify for the next round and now we have to keep going," said Hakimi.
"Haiti were already out of the competition but showed great spirit and made us suffer but in the end we did it."
Haiti had only pride to play for but made Morocco work for their victory and despite losing all three games at the tournament -- their first in 52 years -- will depart with their reputation enhanced.
"We were up against a quality team and I hope we gave our fans something to relish," said Haiti coach Sebastien Migne. "We have to keep working hard to come back again four years from now."
VOCIFEROUS SUPPORT
With vociferous support willing them forward, Haiti opened the scoring after 10 minutes as Josue Casimir calmly shielded the ball, waiting for Jean-Kevin Duverne to support him on the wing and carry it into the box before a square pass was audaciously backheeled goalwards by Lenny Joseph.
FIFA initially awarded the goal to the Haiti striker even though his effort looked to be going wide, deflecting in off the back of Morocco goalkeeper Yacine Bounou, but it was later changed to become yet another own goal in this tournament.
Haiti’s 38-year-old goalkeeper Johny Placide, playing his last international after 15 years in the national team, produced a series of sharp saves to twice deny Ayoub El Kaabi and Hakimi.
But when he parried Bilal El Khannouss’s cross, Hakimi was quickest to bundle it over the line for the equaliser.
Duverne set up Isidor for his rasping strike from outside the area to put Haiti ahead again.
Morocco’s response was much quicker the second time, with Sofyan Amrabat setting Hakimi off down the right flank and the captain dragging the ball back for Saibari to slot home and leave the teams deadlocked at 2-2 by halftime.
VEHEMENT PROTESTS
Haiti's defence was eventually breached again late in the second half from a set-piece, although they protested vehemently for a foul after Rahimi had controlled the ball before turning and slamming home.
Rahimi then set up the baby-faced Yassine for a tap-in after Haiti’s defenders stopped playing, believing the ball had gone out of play, but VAR confirmed the goal.
"We’d have loved to top our group, but I don’t think we played well enough to do so," said Morocco coach Mohamed Ouahbi.
"We had to go all out in the second half and had so many chances to score.”

Mexico bounce Czechs out of World Cup with win
South Africa beat South Korea to reach World Cup knockout stage for first time
Qatar’s Assim Madibo gets 5-game ban for injuring Canada's Ismael Kone
Vinicius fires Brazil into World Cup knockouts with win over Scotland
Bosnia and Herzegovina near advancement after bouncing Qatar
