Emil Forsberg’s second-minute strike looked to have set them on their way before his well-struck second before the hour put them 2-0 up.
But Robert Lewandowski did pull one back almost immediately for his side in St. Petersburg to set up a nervy finale.
United full back Krafth entered the fray as a 68th-minute replacement for forward Alexander Isak, helping to shore up their backline under heavy pressure.
And that pressure eventually told with seven minutes remaining, as Lewandowski’s second completed the comeback.
But Viktor Claessen’s 93rd-minute winner stole the points for the Swedes - and ensured they would top the group ahead of Spain, while Poland are eliminated after finishing fourth.
Krafth and his side can now turn their attentions to the last 16, where they will play one of the best third-placed sides at Hampden Park in Glasgow on Tuesday (kick-off 8pm BST).
Martin Dúbravka, meanwhile, endured a hectic evening, saving a penalty but scoring an own goal as Slovakia’s Euro 2020 hopes ended at the hands of Spain.
The Magpies goalkeeper started in Seville and was called into action just 12 minutes into the final Group E contest when Jakub Hromada was penalised for a foul on Koke in the box.
Alvaro Morata stepped up, but Dúbravka parried his effort from the spot to give his side a lift as they sought a win which would have sent them into the last 16.
But on the half hour, it turned sour for the United man. Former Newcastle defender Lubo Satka’s misplaced pass allowed Pablo Sarabia to take aim, and when his strike flew upwards off the crossbar it looked as though Dúbravka was set to turn it behind for a corner when it eventually dropped.
But he didn’t get enough on it to turn it over the bar, and inadvertently palmed the ball into his own goal to hand Spain the lead.
Luis Enrique’s men doubled their lead before the break through Aymeric Laporte’s header, which looped beyond Dúbravka and into the corner of the net.
And there was no respite for them in the second half, as Sarabia swept home a third ten minutes after the restart and Ferran Torres made it four.
Juraj Kucka’s own goal made it 5-0 and completed the scoring on a miserable evening for Slovakia, who finished third in the group with three points.
But despite their opening-day win over Poland, Slovakia were eliminated, with their goal difference making it impossible for them to progress as one of the best third-placed sides.