Team Canada won its 17th gold medal at the IIHF World Junior Championship on Friday, defeating Sweden 3-1 in a thriller.

Tyler Steenbergen couldn’t have found a more opportune time for his first goal of the tournament, delivering the game-winner with under two minutes to play as he deflected in a shot by Conor Timmins from in front of the net, keeping what had been a tight game from going to overtime.

Team Canada Tyler Steenbergen

Team Canada forward Tyler Steenbergen (17) celebrates his game winning goal against Sweden during third period gold medal final IIHF World Junior Championships hockey action in Buffalo, N.Y., on Friday, January 5, 2018. THE CANADIAN PRESS/Nathan Denette

The very pro-Canadian crowd in Buffalo nearly had reason to celebrate with just under 7:50 left on the clock in the first period when the puck got behind Swedish netminder Filip Gustavsson, but only after the whistle had already been blown.

The Swedes outshot Canada 16-9 in the opening frame, but goalkeeper Carter Hart kept it scoreless as he made several fine saves, including one with just a minute to play. Hart had started five of Canada’s six games before the final, allowing just 10 goals to put his goals against average under two (1.97). That was second-best in the tournament behind only Gustavsson’s nine goals allowed and 1.77 GAA.

Team Canada Carter Hart

Team Canada goaltender Carter Hart (31) makes a save against Sweden during first period gold medal game IIHF World Junior Championship hockey action in Buffalo, N.Y. on Friday, January 5, 2018. THE CANADIAN PRESS/Frank Gunn

With the win, Hart tied Stephane Fiset and Jimmy Waite for the most wins by a Canadian goalie in a world junior championship with eight.

Less than two minutes into the second period, Sam Steel was deep in his own zone when he got the puck to Jordan Kyrou, who made a nifty move to keep it away from an attempted poke check. He then passed to captain Dillon Dube who finished up top to give Canada the 1-0 lead.

That put Sweden behind for the first time all tournament and brought Kyrou’s team-leading points total to 10 (3 goals, 7 assists).

However, down a man with less than seven minutes to play in the second after an interference penalty, Sweden got the equalizer as a 2-on-2 became a 3-on-1 and Tim Soderlund rattled a shot off the pipes and in. It was their third straight shorthanded goal following two in the semifinal.

Team Canada Jordan Kyrou

Team Canada forward Jordan Kyrou (25) carries the puck as Sweden forward Axel Jonsson Fjällby (22) defends during third period gold medal game IIHF World Junior Championship hockey action in Buffalo, N.Y. on Friday, January 5, 2018. THE CANADIAN PRESS/Nathan Denette

Still tied with less than six minutes to go in the third period, Alex Formenton was tripped by Fabian Zetterlund right in front of the net, giving Canada its fifth power play of the game.

Canada had been 13 for 23 (56%) on the power play, far and away leading the tournament, but was ineffective in the gold medal game. A great chance arose when Kyrou got a pass under Gustavsson’s glove to Taylor Raddysh, but he shot it off the post.

But the Canadians got what they were looking for with 1:40 left to go when Steenbergen got his timely first goal.

Formenton sealed it 26 seconds later with an empty netter.

Canada is now 4-0 all-time versus Sweden in world junior gold medal games and moved its record in the tournament to 5-0-1 when scoring first.

Canada last won world junior gold in 2015. Next year’s tournament will take place in Victoria and Vancouver.