Carlos Soler volleyed Luis Enrique’s Spain into an early lead in Stockholm on his international debut.
But just 12 seconds after play resumed Alexander Isak took advantage of a defensive mistake to slam home.
Viktor Claesson controlled Dejan Kulusevski’s pass before firing in a famous winner from 12 yards out.
The previous team to beat Spain in a World Cup qualifier were Sweden’s neighbours, Denmark, in March 1993 in Copenhagen.
Since then Spain had won 52 World Cup qualifiers and drawn the other 14.
The win takes Sweden top of Group B on nine points from three games.