Two first-half goals set Morocco on their way to top place in World Cup Group F but they had to hold on for a 2-1 win over Canada at the Al Thumama Stadium on Thursday after conceding an own goal that kept them nervous through the second half.
A goalkeeping howler allowed Hakim Ziyech to score after four minutes, followed by a second in the 23rd minute from Youssef En-Nesyri, running onto a superb long pass from Achraf Hakimi.
But Nayef Aguerd stuck out a toe to deflect a cross from Sam Adekugbe into his own net five minutes before the break to offer already-eliminated Canada a chance to get back in the game and win a first World Cup point. Canada came close to a second-half equaliser but proved unable to make the breakthrough.
Morocco next play on Tuesday against the runners-up in Group E, which will be decided later on Thursday.
Morocco finished on seven points, two more than runners-up Croatia, to advance from the group stages for a second time, 36 years after they became the first African country to do so at the Mexico World Cup in 1986.
Croatia eased into the last 16 of the World Cup after holding a stuttering Belgium to a 0-0 draw on Thursday, sending their European rivals' 'golden generation' out of the tournament with barely a whimper.