Match ends, Gabon 2, Morocco 2.
Morocco drew 2-2 with Gabon in Yaounde in one of the best games of the Africa Cup of Nations so far as both sides progressed from Group C.
The Atlas Lions fell behind in the first half as Gabon's Jim Allevinah slid past the onrushing keeper.
Morocco's equaliser from the penalty spot by Sofiane Boufal after 74 minutes sparked a thrilling finale.
First, Gabon forced Nayef Aguerd into scoring an own goal before Achraf Hakimi curled home a free-kick.
Morocco will stay in the Cameroonian capital for their next game, against the best third-placed side from either Group A, B or F, on 25 January.
Gabon, meanwhile, will face Burkina Faso in Limbe as both sides kick off the Round of 16 on Sunday 23 January.
Gabon impress in Yaounde
Having exited in the group stage as hosts in 2017 and missed the 2019 finals altogether, Gabon were hugely impressive when reaching the knock-out stage for only the second time in the past 25 years.
They took the game to a Moroccan side who rested six players from the team that secured qualification for the knock-outs when beating Comoros 2-0 last time out.
Chance upon chance came their way with Aaron Boupendza, who was lively throughout, toe-poking wide just after the half-hour when it looked easier to hit the target.
Seven minutes before the break, Guelor Kanga hit a blistering free-kick from 40 yards that forced reserve keeper Monir El Kajoui to tip over the bar - just as he had to from the resulting corner when denying Lloyd Palun's firm header with a fine stop.
Despite missing Pierre-Emerick Aubameyang, who had to take to social media to deny reports he had been out drinking prior to being sent back to England for checks on heart lesions seemingly developed after a Covid infection, Gabon continued to threaten.
Allevinah almost scored one of the goals of any Nations Cup when meeting a deep cross from a tight angle to fire against the bar with a strike that brought back memories of Marco van Basten's stunning 1988 European Championship final goal.
Morocco stir to clinch top spot
Morocco had enjoyed the greater possession in the first half without forcing a shot on target, but this changed as a worried-looking Atlas Lions coach Vahid Halilhodzic rang the changes.
The increased threat brought a penalty converted by Boufal after he had won the spot-kick, even if it seemed that Andre Biyogo Poko had won the ball.
After video assistant referee Janny Sikazwe, who was back after reported heatstroke during his calamitous display when blowing for full-time early in the Tunisia-Mali game, failed to question the decision, Morocco were back in the game.
Seconds later, Boupendza flashed a strike past the post before he then ran off in wild celebration to the hordes of Gabonese fans, jumping the advertising boards as he did so, after he pressured Aguerd into turning home Allevinah's smart cut-back nine minutes from time.
Gabon were top of Group C for the second time on the night only for Biyogo Poko to concede another set-piece, which Paris Saint-Germain star Hakimi converted with a mesmeric free-kick that left Gabon's goalkeeper grasping thin air.
It was a fitting end to a thrilling game as Group C delivered goals aplenty to a Nations Cup criticised for a lack of them, and ensured that Morocco - looking to reach the final for the first time since finishing runners-up in 2004 - proceed into the same half of the draw as hosts Cameroon.
- 5Ecuele Manga
- 2Moucketou-MoussoundaSubstituted forAmekaat 64'minutes
- 17Biyogo Poko
- 6ObiangBooked at 36mins
- 11AllevinahSubstituted forMayiat 88'minutes
- 7BoupendzaBooked at 45minsSubstituted forMéyéat 90+1'minutes
- 16Mfa Mezui
- 18ChaklaSubstituted forSaïssat 89'minutes
- 8OunahiSubstituted forTissoudaliat 89'minutes
- 13ChairSubstituted forAmallahat 31'minutes
- 4S AmrabatBooked at 38mins
- 11FajrSubstituted forBarkokat 57'minutes
- 9El KaabiSubstituted forBoufalat 57'minutes
- Dahane Beida
- Shots on Target
Second Half ends, Gabon 2, Morocco 2.
Offside, Gabon. André Biyogo Poko tries a through ball, but Lloyd Palun is caught offside.
Attempt blocked. Guélor Kanga (Gabon) right footed shot from outside the box is blocked.
Foul by Romain Saïss (Morocco).
Axel Méyé (Gabon) wins a free kick on the left wing.
Substitution, Gabon. Axel Méyé replaces Aaron Salem Boupendza.
Substitution, Morocco. Romain Saïss replaces Sofiane Chakla.
Substitution, Morocco. Tarik Tissoudali replaces Azzedine Ounahi.
Substitution, Gabon. Kévin Mayi replaces Jim Allevinah.
Foul by Adam Masina (Morocco).
Anthony Oyono (Gabon) wins a free kick in the defensive half.
Sofyan Amrabat (Morocco) wins a free kick in the defensive half.
Foul by Johann Obiang (Gabon).
Goal! Gabon 2, Morocco 2. Achraf Hakimi (Morocco) from a free kick with a right footed shot to the top left corner.
Sofiane Boufal (Morocco) wins a free kick in the attacking half.
Foul by André Biyogo Poko (Gabon).
Own Goal by Nayef Aguerd, Morocco. Gabon 2, Morocco 1.
Attempt missed. Selim Amallah (Morocco) right footed shot from outside the box is high and wide to the right. Assisted by Adam Masina.
Foul by Aymen Barkok (Morocco).