The two sides drew 0-0 during Euro 2020 but in front of a vibrant home support, Sweden was far more of a threat going forward this time, with star duo Dejan Kulusevski and Alexander Isak playing decisive roles.
Isak got the swift equaliser after Carlos Soler netted a debut goal early on in a largely enjoyable but undramatic first half.
The second period was considerably more exhilarating, and it was fine work by Kulusevski that created Viktor Claesson's winner, which leaves Sweden top of the group with nine points, two clear of Spain who have also played a game more.
Soler's international career was less than four minutes old when he turned in Jordi Alba's pinpoint delivery at the end of a flowing move, but their lead lasted only 63 seconds.
An error by Soler in the Spain midfield allowed Isak to seize possession and he clinically found the bottom-right corner from 20 yards.
Spain predictably dominated possession but did not create any other clear-cut chances before the break, while at the other end they needed Unai Simon alert as he blocked Kulusevski's effort from a tight angle in the 36th minute.
Sweden had a lucky escape just after the restart, Robin Olsen saving from point-blank range when Filip Helander diverted a Cesar Azpilicueta cross towards his own goal.
They soon made the most of that let-off by going 2-1 up.
Kulusevski beat Eric Garcia on the left and pulled a wonderful pass back to the centre of the box for Claesson, whose disguised finish found the bottom-left corner.
Spain piled the pressure on in the final stages, substitute Adama Traore particularly lively on the right, but ultimately Sweden held on for a deserved victory.