Hosts Russia advanced to the World Cup quarterfinals on penalties after a 1-1 draw on a dramatic afternoon in Moscow.
Sergei Ignashevish's own goal gave Spain an early lead before Artem Dzyuba levelled, but neither side could find another breakthrough in extra time and Igor Akinfeev was Russia's hero, denying Koke and then diverting Iago Aspas's effort away with his foot in a dramatic shootout.
Spain, who parted ways with coach Julen Lopetegui just days before the tournament, joined England and Italy as the only teams to lose three shootouts at the World Cup, having been knocked out by hosts South Korea in 2002 and Belgium in 1986, both in the quarterfinals.
Hosts Russia had pressed Spain in the opening stages, Aleksandr Golovin getting away down the right to win an early corner that came to nothing.
Early flickers from Spain came through Isco's measured passing, but when their opening goal arrived after 11 minutes it came from a set piece.
Yuri Zhirkov gave away a free kick when he fouled Nacho on the right and, when Aesnsio curled the cross in, Ignashevish tangled with Sergio Ramos at the far post and the ball flew in off the defender.
Russia tried to hit back when Zhirkov got down the left, but the move fizzled out as home fans at the Luzhniki Stadium tried to digest their side's early setback.
Spain, buoyed by their breakthrough, were beginning to dominate possession with Russia struggling to find a foothold, but the hosts were briefly boosted when an Dzyuba header from a long free kick caused confusion before Spain whacked it clear.
Isco almost freed Jordi Alba down the right only to overhit the pass, before Mario Fernandes and Aleksandr Samedov embarked on a promising combination before the move fizzled out.
With 36 minutes gone they created a good chance, the ball worked to the edge of the area where Golovin made his way into a pocket of space before bending his strike wide.
That proved the cue for a good spell of possession as the first half entered its final five minutes, which brought another corner when a cross cannoned off Koke and behind.
And from it, they won a penalty when Dzubya leapt to win the ball and it hit Pique's raised hand -- and after a moment's hesitation, the referee pointed to the spot and booked the defender.
Dzubya stepped up to blast nervelessly past David De Gea and level the match after 41 minutes, and there was still time for Isco to see a shot blocked and Diego Costa to force Akinfeev into action from an Isco cross.
Russia took off Zhirkov and brought on central defender Vladimir Granat at the break, but Spain had the first chance of the second half when Marco Asensio's cross was deflected goalwards by Alba and saved by Akinfeev.
Alba and David Silva then tried to combine, but fine defending from Ilya Kutepov stopped them before Kutepov was booked for a foul on Isco. Spain's first corner of the match followed after 59 minutes, but Isco's cross was headed away by Dzubya.
Russia made another change soon after the hour, taking off Samedov and bringing on Denis Cheryshev, and their third switch followed after 65 minutes, scorer Dzyuba off for Fyodor Smolov.
Andres Iniesta came on for Silva and Nacho was replaced by Dani Carvajal in another Spain switch with 20 minutes remaining.
Spain were attempting to be patient but offered little or nothing in the way of a final pass as an increasingly tight game entered the final 10 minutes of normal time, Costa making way for Aspas in their third change.
With five minutes to go they were agonisingly close to a winner as Iniesta latched on to a clever touch from Aspas and his low strike from the edge of the area hit the post.
Pique leapt to meet a Koke corner, his header deflecting over the bar for another as Spain mounted some late pressure, and Smolov blasted wide as four minutes of added time ticked away with neither side able to find a winner.
Aspas carved out an early chance in extra time with a run and cross from the left, but Carvajal was unable to turn it in and then Isco's low centre created another, Ignashevich clearing.
Russia introduced Aleksandr Yerokhin for Daler Kuzyayev -- the first time ever that a fourth substitute had been used at the World Cup -- and Asensio fired straight at Akinfeev from the edge of the area before coming off for Rodrigo as Spain also used their fourth sub.
With four minutes of the second period of extra time played, Rodrigo ran at the Russia defence but Akinfeev saved his shot, with Carvajal's follow-up blocked.
Aspas was unable to get the ball under control after Iniesta had threaded a pass through into the area, with Spain pinning Russia deep inside their own half but still unable to find a way through.
Spain appealed loudly for a penalty when Pique appeared to be held from a deep Koke free kick but, after a VAR check, the referee signalled for play to continue and at the other end Sergio Busquets dawdled but, after Smolov had taken possession, his pass was woeful.
Akinfeev dived to save a Rodrigo shot in the one added minute at the end of extra time to huge cheers from the crowd as the game went to penalties.
Spain took the first, Iniesta scoring, before Smolov followed his example -- despite De Gea getting a faint touch -- and then Pique scored via the inside of the post and Ignashevish sent De Gea the wrong way.
Koke's penalty was saved by Akinfeev and Golovin stepped up to smash home and give the hosts the advantage, but Ramos kept Spain in it before Cheryshev put the hosts on the brink of victory -- and then Akinfeev saved Aspas' effort with his foot to send them through to the next round, where they will face Croatia.