Match ends, Croatia 2, Spain 1.
Spain will play Italy in the last 16 of Euro 2016 after a late Croatia winner condemned them to second in Group D.
Spain took the lead when Cesc Fabregas dinked the ball over keeper Danijel Subasic and Alvaro Morata tapped in.
An Ivan Rakitic lob hit the woodwork for Croatia, who equalised when Nikola Kalinic turned in Ivan Perisic's cross.
Subasic saved a Sergio Ramos penalty and Perisic's late winner gave Croatia, who finished top of the group, a tie against a third-placed qualifier.
Perisic lashed in an 87th-minute shot in a thrilling match to inflict Spain's first defeat in 15 games at the European Championship since losing to Portugal in Euro 2004.
Croatia will now play on Saturday in Lens, while Spain's mouth-watering tie against Italy - a repeat of the 2012 final - will take place on Monday in Paris.
Spain's defence looks suspect
This was an eagerly anticipated game because of the quality at Croatia's disposal and the type of test they were expected to give the reigning champions.
From that point of view, Croatia pressed and pushed further up than Spain's previous opponents - Turkey and the Czech Republic - and, despite going behind, caused plenty of moments of uncertainty at the back for Vicente del Bosque's side.
Spain centre-back Ramos gave the ball away early on and Perisic had a shot palmed away by keeper David de Gea.
De Gea was then dispossessed by Kalinic and the ball fell to Rakitic, whose chip hit the crossbar and the post before bouncing the wrong side of the post for Croatia.
Spain again dominated possession but their resistance was finally broken when they conceded their first goal in 735 minutes at the Euros as Kalinic stole in front of Ramos to turn in Perisic's left-wing cross.
A weak punch from De Gea led to him having to save Tin Jedvaj's shot before Marko Pjaca sent an overhead kick wide.
Spain's keeper and their defence looked vulnerable when exposed before they were caught out on the counter-attack, with Perisic's strike beating De Gea at his near post.
Croatia's strength in depth
Croatia coach Ante Cacic made five changes while his team were without key midfielder Luka Modric and striker Mario Mandzukic through injury.
Nevertheless, they managed to provide further evidence they pose a danger to any team in the tournament by not only showing character to come back from a goal down but discipline in defence and a threat when going forward.
Perisic and captain Darijo Srna are key to their play with their energetic running on the flanks and ability to whip in dangerous crosses, like the one which led to the equaliser.
Croatia, who left Ramos unmarked twice from crosses only for him to fail to get his headers on target, were angered by the penalty award for Sime Vrsaljko's challenge on David Silva and will have felt justice was done when Subasic, who was three yards off his goal-line, saved.
In another plus for Croatia, there was no trouble from their fans following the crowd unrest and throwing of flares on to the pitch which marred their draw with Czech Republic.
Man of the match - Ivan Perisic
'We have to rise to the situation'
Spain manager Vicente del Bosque: "The players were sat in silence. They don't enjoy losing. We had the game under control, I'm not going to defend our performance excessively but I think we played quite well.
"We didn't hold on to the ball as well as we should have [before the winning goal] and weren't ready for the counter attack. But we're still in the competition, we are not on the path we wanted to be but we have to rise to the situation."
Croatia boss Ante Cacic: "This team is like our nation. The players have shown how to represent your country and how to behave.
"I wanted to play the next match with really fresh players. It's a huge achievement, and now we can be even more self-confident."
Former Wales forward and BBC Radio 5 live summariser John Hartson: "I think Croatia have deserved it, Spain have played some good football, easy on the eye, but they were not clinical enough in front of goal."
The stats you need to know
- This is the first time Spain have ever lost a game in a Euro finals tournament after being ahead.
- Sergio Ramos is the first Spanish player to miss a penalty at a European Championship since Raul v France in 2000.
- Cesc Fabregas has been involved in more goals than any other Spaniard at Euro finals (three goals, five assists).
- 11SrnaBooked at 70mins
- 2VrsaljkoBooked at 70mins
- 15RogBooked at 29minsSubstituted forKovacicat 82'minutes
- 4PerisicBooked at 88minsSubstituted forKramaricat 90+4'minutes
- 20PjacaSubstituted forCopat 90+2'minutes
- 16N Kalinic
- 12L Kalinic
- 13de Gea
- 16Torres Belén
- 10FàbregasSubstituted forThiago Alcántaraat 84'minutes
- 7MorataSubstituted forAdurizat 67'minutes
- 22NolitoSubstituted forBrunoat 60'minutes
- 14Thiago Alcántara
- 17San José
- Björn Kuipers
- Shots on Target
Second Half ends, Croatia 2, Spain 1.
Offside, Croatia. Ivan Rakitic tries a through ball, but Duje Cop is caught offside.
Substitution, Croatia. Andrej Kramaric replaces Ivan Perisic.
Substitution, Croatia. Duje Cop replaces Marko Pjaca.
Foul by Sergio Busquets (Spain).
Sime Vrsaljko (Croatia) wins a free kick in the defensive half.
Sergio Ramos (Spain) wins a free kick in the defensive half.
Foul by Nikola Kalinic (Croatia).
Ivan Perisic (Croatia) is shown the yellow card for excessive celebration.
Goal! Croatia 2, Spain 1. Ivan Perisic (Croatia) left footed shot from the left side of the box to the centre of the goal. Assisted by Nikola Kalinic following a fast break.
Attempt blocked. Aduriz (Spain) right footed shot from outside the box is blocked. Assisted by Sergio Busquets.
Substitution, Spain. Thiago Alcántara replaces Cesc Fàbregas.
Foul by Cesc Fàbregas (Spain).
Darijo Srna (Croatia) wins a free kick in the defensive half.
Substitution, Croatia. Mateo Kovacic replaces Marko Rog.
Delay over. They are ready to continue.
Delay in match Vedran Corluka (Croatia) because of an injury.
Attempt missed. Sergio Ramos (Spain) header from the centre of the box is too high. Assisted by David Silva with a cross following a corner.
Corner, Spain. Conceded by Darijo Srna.