Alvaro Morata and Mikel Oyarzabal scored extra-time goals to earn Spain a 5-3 win over Croatia in Monday's breathless Euro 2020 last-16 tie after La Roja had squandered a two-goal lead late on in normal time.
Spain recovered from a bizarre Pedri own goal at Parken Stadium thanks to strikes from Pablo Sarabia, Cesar Azpilicueta and Ferran Torres to lead 3-1 with 85 minutes played.
However, Mislav Orsic bundled in to give Croatia a lifeline in Copenhagen and then set up fellow substitute Mario Pasalic in the 92nd minute to take an enthralling game to extra time.
But Spain dug deep in the additional period to book a place in the quarter-finals, where either France or Switzerland await, with Morata making amends for an earlier miss with a well-taken goal and Oyarzabal adding a second from the bench to settle a game that will go down as a European Championship classic.
Profligacy cost Spain in their opening two group games and that looked like being the case again in Copenhagen after Koke was denied by Dominik Livakovic from a one-on-one and Morata headed into Domagoj Vida from close range.
A comical moment followed as Pedri's pass from 49.4 yards was miscontrolled by Unai Simon and rolled into the back of the net, making it the longest-range own goal in European Championship history.
Luis Enrique's men responded well by equalising before half-time through Sarabia's powerful drive after Jose Gaya's shot was parried into his path by Livakovic.
Azpilicueta put Spain in front with his first international goal by getting in front of Josko Gvardiol and guiding Torres' cross past Livakovic from inside the six-yard box.
Spain gave themselves breathing space nine minutes later as Torres cut inside and clinically finished off a swift move.
With five minutes to go, though, Orsic followed up after a scramble in the box to help the ball over the line – the goal allowed to stand following a VAR check for handball inside the box.
Then came the dramatic leveller in added time, with Pasalic left in space inside the box to head in Orsic's cross and pave the way for an additional 30 minutes.
Croatia started the period of extra time on top and would have taken the lead if not for an incredible Simon stop to keep out Andrej Kramaric, but Morata took down Dani Olmo's cross at the back post and thumped past Livakovic four minutes later.
Super sub Olmo then provided the cross for fellow replacement Oyarzabal, who also hit the post in the final seconds, to tuck home the eighth goal of a sensational European Championship tie and confirm Spain's place in the last eight.