Rodrigo Moreno came off the bench to snatch a last-gasp 1-1 draw in Sweden and confirm Spain's place at Euro 2020.
Inspired saves from Robin Olsen and an injury to their own goalkeeper, David de Gea, added up to what was starting to look like a wretched outing at the Friends Arena for Robert Moreno's men.
But Rodrigo, who was dropped to the bench as his coach made six changes to the XI held by Norway last time out, reacted sharply to prod in from close range.
Sweden rode out an early storm to hit the front through Marcus Berg in the 50th minute and still has its fate in its own hands ahead of pivotal trip to Romania next month.
Sweden was indebted to Olsen during the early stages, when Spain's masterful control of possession pinned the host deep inside its own territory.
Each member of Spain's central midfield trio – Fabian Ruiz, Thiago Alcantara and Rodri – were denied by Olsen, although Thiago opting to try and round the goalkeeper in the eighth minute amounted to a glaring error when a square pass or a finish looked simple.
Olsen pushed a header from Spain debutant Gerard Moreno behind before Sweden belatedly found its feet - De Gea impressively saving from Robin Quaison in the 33rd minute after the Mainz striker prodded an earlier opener wide.
Those moments of encouragement proved instructive and Jan Olof Andersson's side hit the front when Berg bundled home, De Gea having completed a stunning double save from the eventual goalscorer and Emil Forsberg in vain.
Forsberg tested the Manchester United number one shortly afterwards with Spain reeling – De Gea's departure after an hour doing little to improve his country's prospects, or indeed raise optimism levels at Old Trafford ahead of Liverpool's visit on Sunday.
Gerard was increasingly feeding off scraps as a fractious contest played into Sweden's hands but glanced wide in the 67th minute.
Olsen then thwarted Mikel Oyarzabal impressively, although he could do nothing when substitute Alexander Isak misjudged a headed clearance and Fabian drilled back across goal for Rodrigo to apply the finish.
What does it mean? Job done for Moreno in campaign hit by tragedy
As has frequently been the case over recent years, this was not Spain at its fluent best. But Moreno can reflect on mission complete in a job that came to him in tragic circumstances following Luis Enrique's decision to step down.
Olsen's masterclass deserved more
Sweden's goalkeeper did not have the luxury of feeling his way into the contest, as many of those in front of them did. Without Olsen's razor-sharp reflexes, a victory snatched away would never have been on the agenda.
Villarreal star Gerard gives mixed audition
Gerard battled gamely against Victor Lindelof and Andreas Granqvist but an attack that has been led by Rodrigo, Alvaro Morata and Paco Alcacer at various points during this qualifying campaign looked no closer to finding the missing piece before the Valencia man stole in to grab the headlines.
Sweden is a point ahead of Romania, which it has already beaten. A draw in Bucharest should be enough with the Faroe Islands up last. If Sweden falters next time out, though, Spain could be the kingmaker as it finishes the campaign against Romania after taking on Malta.