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.
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.
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.