Ivan Perisic struck a sensational winner three minutes from time as Croatia ended Spain's 14-game unbeaten run at the European Championship to top Group D after a 2-1 victory in Bordeaux 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 key man 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.
The manner of back-to-back victories over the Czech Republic and Turkey had seen Spain installed as favourites to win an unprecedented third successive European Championship.
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 despite the absence of 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, Vicente Del Bosque's men were pegged back right on half-time when Perisic's cross from the left was delightfully 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 seemed to knock 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 responded by withdrawing 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 penalty 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 leaving Spain, Italy, Germany, France and England all on the bottom-half of the last-16 draw.