Holders Ivory Coast were knocked out of the Africa Cup of Nations following a 1-0 defeat in Oyem by Morocco, who progressed to the quarter-finals.
The Elephants had drawn their first two Group C matches, leaving them needing to beat Morocco and former coach Herve Renard, who had guided the squad to glory in 2015 when winning a penalty shootout over Ghana in Equatorial Guinea.
However, after wasting chances to get ahead before half-time – with Crystal Palace forward Wilfried Zaha and former Chelsea frontman Salomon Kalou both failing to find the net – it was the Atlas Lions who swept into the lead on 64 minutes through Rachid Alioui.
Ivory Coast failed to conjure a way back into the match, as Morocco held out to qualify in second place behind DR Congo, who beat Togo 3-1 in Port-Gentil.
Morocco had lost their opening match of the tournament to DR Congo, but recovered with a 3-1 victory against the Sparrow Hawks.
On 24 minutes, Renard’s side went close to taking the lead as a free-kick from Faycal Fajr hit the underside of the crossbar and bounced away.
Ivory Coast created a clear opening in the 36th minute when Hertha forward Kalou fed Zaha on the left of the penalty area, but his angled shot was beaten away by the goalkeeper.
Then just before the half-time interval, Stoke forward Wilfried Bony glanced a header across the penalty area, which dropped towards the far corner, where Kalou could only nod it onto the outside of the post.
Morocco made the Elephants pay when they went ahead just after the hour mark.
There looked little on as Alioui picked the ball up after a break down the left. However, the Nimes forward held up possession before lofting a superb chip over Ivory Coast goalkeeper Sylvain Gbohouo and into the roof of the net.
Elephants manager Michel Dussuyer sent on Aston Villa’s Jonathan Kodjia, replacing Zaha, and then Bournemouth midfielder Max Gradel for the closing stages, but there was no way back for the champions.
Indeed, Morocco could have added a late second on the break through Hamza Mendyl, but the 19-year-old full-back fired into the side netting after bursting clear down the left.