Iago Aspas scored a stoppage-time equaliser - with the aid of VAR - as Spain scraped an unconvincing draw with Morocco to top Group B and secure a last-16 tie against World Cup hosts Russia.
Youssef En-Nesyri - who plays in Spain for Malaga - looked to have won it for Herve Renard's side when he powered in a superb header from fellow substitute Faycal Fajr's corner.
But Aspas flicked in Dani Carvajal's low cross to make it 2-2, with referee Ravshan Irmatov initially disallowing the goal before overturning the decision.
Spain won the group on goals scored as Portugal were held to a 1-1 draw by Iran - but had a tough night.
A dreadful mix-up between Andres Iniesta and Sergio Ramos had allowed Khalid Boutaib to stride through and give Morocco a surprise 14th-minute lead.
Iniesta made amends five minutes later by helping create a sublime equaliser, exchanging passes with Diego Costa and getting to the byeline before cutting back for Isco to steer high into the net.
But Spain looked wobbly in defence when pressed by a committed, aggressive Morocco side, with Boutaib denied by the legs of David de Gea after Hakim Ziyech's quick throw sent him clear.
Nordin Amrabat was desperately unlucky not to score in the second half as his searing shot came back off the angle of post and bar with De Gea motionless.
Isco was denied a second goal when his header was diverted wide by Romain Saiss - playing instead of dropped captain Medhi Benatia - before the late drama.
Latest Stories
-
Appoint ‘brutal’ OPK as Whip – Carlos Ahenkorah urges NPP leadership
5 minutes -
Ablakwa questions alleged GH₵60m transfer to presidency for ‘special operations’
53 minutes -
Dame Judi reveals apple tribute to Maggie Smith
55 minutes -
NDC rejects rerun in outstanding polling station in Dome Kwabenya
1 hour -
EC has the power to re-collate results – Ayikoi Otoo
1 hour -
Putin apologises for plane crash, without saying Russia at fault
1 hour -
Footage shows NY officers beating prisoner before death
1 hour -
AngloGold Ashanti equips mastercraft persons at Sanso with tools to train youth apprentices
1 hour -
Okokobioko delivers resounding performance at 1031 Laughs & Music
1 hour -
Fatawu Issahaku’s knee injury a big blow for Leicester – Van Nistelrooy
2 hours -
EC to rerun parliamentary election at outstanding polling station in Dome Kwabenya
3 hours -
Liverpool manager hails Mohammed Kudus ahead of West Ham clash
3 hours -
Final declaration of Dome-Kwabenya parliamentary results suspended for the 3rd time amid chaos
3 hours -
Mahama’s one-term a golden opportunity to improve governance – Kyei-Mensah-Bonsu on constitutional review
3 hours -
Saka faces ‘more than two months’ out after surgery – Arteta
3 hours