Ivan Perisic struck a sensational winner three minutes from time as Croatia ended Spain's 14-game unbeaten run at the European Championship finals to top Group D after a 2-1 victory on Tuesday.
Spain will now face Italy in the last 16 in Paris on Monday in a repeat of the 2012 final.
Alvaro Morata's early strike got Spain off to a flying start. But despite the absence of Luka Modric through injury, Croatia battled back to equalise before half-time through Nikola Kalinic.
Spain captain Sergio Ramos saw a penalty saved by Danijel Subasic 18 minutes from time.
And that miss proved vital when Perisic snuck the ball home at David de Gea's near post.
Croatia meet one of the best third-placed sides in Lens on Saturday.
Spain could also have to face world champions Germany in the quarter-finals and hosts France in the semi-finals if they are to reach the final in Paris on July 10.
"Now we have to dance with the favourites, but to be champions you have to beat everyone," said Ramos, who vowed Spain will learn from their mistakes.
Coach Vicente del Bosque would rather have avoided an early meeting with the on-form Italians.
"It is not ideal, that would have been to finish first (in the group)," said Del Bosque.
"We have to recover well. We are prepared to turn this situation around."
Morata's return to European champions Real Madrid from Juventus had been confirmed earlier on Tuesday.
And he moved level with club teammate Gareth Bale on three goals in the fight for the golden boot by finishing off a slick Spanish move after seven minutes.
David Silva's cute reverse pass picked out Cesc Fabregas's run and the Chelsea midfielder's cross was tapped into an empty net by Morata.
Spain looked rarely troubled at the back in their opening two games, but were given plenty of problems by Croatia even without star midfielder Modric.
Kalinic stung the palms of David de Gea before forcing an error from the Manchester United 'keeper as he dallied.
The loose ball fell to Ivan Rakitic and his brilliant chipped effort floated over Barcelona teammate Gerard Pique on the line before coming back off both the crossbar and post.
Morata had a great chance to double Spain's lead when he failed to control Fabregas's fine through ball with just Subasic to beat.
However, Del Bosque's men were pegged back right on half-time when Perisic's cross from the left was flicked home by Kalinic.
It was the first time Spain had conceded in 11 competitive games stretching back to October 2014 and first time they had conceded in the Euros for a record 733 minutes.
The goal knocked Spain out of their stride as Croatia pushed them onto the back foot at the start of the second period.
De Gea saved from Tin Jedvaj to atone for his own error in flapping at a cross before Marko Pjaca sent an acrobatic effort just wide from the rebound.
Pjaca also had claims for a penalty waved away as Croatia threatened the goal that would have taken them top of the group.
Del Bosque withdrew front two Nolito and Morata for the more physical presence of Bruno Soriano and Aritz Aduriz.
Spain were then handed a great chance to retake the lead when referee Bjorn Kuipers adjudged Silva had been pushed by Sime Vrsaljko inside the area.
Modric appeared to send on a message to Subasic when he saw his Real Madrid teammate Ramos step up to take the spot-kick.
And Subasic got lucky as his save was allowed to stand despite the fact he had encroached well before Ramos took aim.
Croatia took full advantage to blow the tournament wide open as Perisic lashed home to round off a clinical counter-attack.