Nicklas Backström hit the winning penalty after it finished 1-1 over the three periods as Ryan O'Reilly equalised for Canada after Victor Hedman had put Sweden ahead.
In the shootout, both Backström and Oliver Ekman-Larsson nailed their penalties for Sweden to the delight of their travelling fans in Cologne.
Canada's quartet of Nate Mackinnon, Brayden Point, O'Reilly and Mitch Marner all failed with their penalty attempts to hand Sweden their first world title since 2013.
“It was a tight game out there, it could have gone either way,” said Washington Capitals centre Backström.
“(Sweden goalkeeper) Henrik Lundqvist kept it tight at the back for us and that made the difference.”
“I have been saying for a couple of years in the NHL that winning like this is a lottery, but this is the way it is.”
“We'll take it, we have the gold medal and we're happy about it.”
Sweden coach Rikard Grönborg said it was fitting that the fine margins decided the gold medal match.
“Even in over-time, it was even in terms of shots on goal,” said the 48-year-old Sweden boss.
“We're very happy, it's huge for us to be world champs after a few near misses.
“Canada is a tremendous team, so my hat's off to them.”
Canada coach Jon Cooper said his side, who came from behind to beat Russia in the semi-finals, had done him proud despite being denied a third-straight world title.
“I want to congratulate Sweden, they are an exceptional team and it was a lot of fun to play them,” said Cooper.
“It was an even-stevens game, we don't feel like we lost a hockey game – we feel like we lost a shootout.”
“I told my guys to hang their heads high, it came to a skill competition and they were simply better than us.”
Excellent defence from both teams added to the tension in a nervy final.
After Canada's Mark Scheifele clipped the post in the first period, Jeff Skinner smacked his shot off the goal's frame in the second as Sweden survived two let-offs.
Despite having Backström in the penalty box for slashing, Sweden took the lead against the run of play 21 seconds before the end of the second period.
Hedman's speculative long-range shot passed two team-mates and hit the back of the net with the puck passing through the legs of Canada's goaltender Calvin Pickard.
The defending champions levelled with a powerplay goal 1:58 into the third period when Ryan O'Reilly flicked home Mitch Marner's long-range shot.
There was no score in over-time and it was Sweden who held their nerve in the shootout.
Earlier, Russia survived a late fightback by Finland as Nikita Kucherov sealed their 5-3 win to claim bronze.
Russia raced into a 4-0 lead with Nikita Gusev (twice), Vladimir Tkachyov and Bogdan Kiselevich all scoring.
But Finland rallied with three unanswered goals through Mikko Rantanen, Mikko Lehtonen and Veli-Matti Savinainen to pull it back to 4-3 early in the third quarter.
However, Kucherov settled Russian nerves though with the team's fifth goal to secure third place.