(CNN)Spain overcame conceding a remarkable own goal to edge past Croatia in extra-time, winning 5-3 in an incredible encounter to reach the Euro 2020 quarterfinals.
Three-time European Championship winner Spain had dominated the opening exchanges, but goalkeeper Unai Simon's incredible error -- a miscontrol from Pedri's back pass -- gifted Croatia a surprise lead.
However, goals from Pablo Sarabia, Cesar Azpilicueta and Ferran Torres turned the game on its head and gave Spain a two-goal cushion heading into the final five minutes.
Somehow, Croatia mounted an unlikely late recovery to level the scores and take the tie to extra-time, but Spain once again reasserted its dominance to win its first knockout match at a major competition since it lifted the European Championship in 2012.
There was certainly no hint in the match's early stages that this would turn into an all-time classic.
Koke had a wonderful chance to give Spain the lead on 15 minutes after latching onto Sergio Busquet's delightful through ball, but he was only able to fire straight at the legs of Dominik Livakovic in the Croatia goal.
Just moments later, Torres' pinpoint cross picked out Alvaro Morata unmarked in front of goal, but the striker skewed his effort horribly -- something that has become a running theme for the 28-year-old at Euro 2020.
However, unfortunately for Spanish fans, that was far from the biggest error one of their players would make in this round of 16 match.
On the 20-minute mark, Pedri's seemingly innocuous back pass was miscontrolled by goalkeeper Simon, who could only watch on in despair as the ball bobbled past his foot and nestled into the corner of the net.
It was the ninth own goal scored at Euro 2020, remarkably equaling the total number of own goals scored at all European Championships between 1960 and 2016.
It would be something of an understatement to say that goal came against the run of play, as Spain had been totally dominant until Simon's calamitous error.
The own goal seemed to briefly give Croatia some confidence, with Spain's players scarcely believing they were behind.
However, Spain soon reasserted its dominance and hit back through Sarabia, who found the roof of the net after Livakovic had initially pulled off a sharp save.
Spain will have entered the halftime break perhaps both relieved and frustrated to be level, but it took little more than 10 minutes for Luis Enrique's side to take the lead after the restart.
Torres' inviting cross was attacked with intent at the far post by Azpilicueta, whose firm header gave Livakovic no chance.
The next 20 minutes provided some sort of redemption for Simon, who pulled off two smart saves to maintain his team's slender advantage.
Those stops paved the way for Spain to extend its lead, as Torres capitalized on some sloppy defending to get in behind Croatia's back line and slot the ball through Livakovic's legs.
Just when it looked as though Croatia was dead and buried, this experienced and resilient team -- World Cup finalist three years ago -- fought back and halved the deficit through Mislav Orsic, with the goal confirmed using goal-line technology.
The announcement that there would be six minutes of added time brought the thousands of Croatian fans to their feet, all of them now sensing a remarkable comeback could be on the cards.
Indeed, Croatia needed less than two of those six minutes to level the scores in incredible fashion, Orsic this time turning provider as his whipped cross was glanced in by Mario Pasalic.
Trailing by two goals with less than five minutes of regulation time remaining, Croatia now found itself on the front foot heading into extra-time.
But Simon's transformation from villain to hero was finally complete when he pulled off a stunning save to deny Andrej Kramaric.
Then, in the space of just three minutes, Spain cemented its place in the quarterfinals with goals from Morata -- a stunning half-volley -- and Mikel Oyarzabal.
Another two-goal deficit proved too much of a mountain to climb for Croatia.
Though often sloppy in defense, Spain showed impressive mental fortitude to overcome Croatia which, at times, looked as though it would be the country heading through to the quarterfinals.