Luis Enrique's Luis Enrique's Carlos Soler fired an early lead in Stockholm on his international debut.
But just 12 seconds after play resumed, Alexander Isaacs took advantage of a defensive blunder to smash into the net.
Victor Clisson controlled Dejan Kulusevski's pass before firing a famous winner from 12 yards.
The previous team that beat Spain in the World Cup qualifiers was Sweden's neighbor, Denmark, in March 1993 in Copenhagen.
Since then, Spain has won 52 World Cup qualifiers and drawn 14.
The win made Sweden top Group B with nine points from three matches.
Spain has seven points from four matches and could face a playoff for a place in the 2022 World Cup as only the group winners qualify automatically.