BOSTON -- James van Riemsdyk scored with 1:36 remaining to give the Toronto Maple Leafs a 6-5 win against the Boston Bruins at TD Garden on Saturday.
Van Riemsdyk scored 1:18 after Patrice Bergeron tied it 5-5.
"I think in times like this you've got to stick together as a team and I think we did that for the whole game," van Riemsdyk said. "We played hard, and again we kept coming back when things didn't go our way, and that was good."
WATCH: All Maple Leafs vs. Bruins highlights
William Nylander had his first NHL hat trick for Toronto (24-17-9), which ended a three-game losing streak.
The Maple Leafs scored three goals in a 1:46 span of the second period to take a 4-1 lead, but the Bruins tied it on a goal by Ryan Spooner at 10:06 of the third. Connor Brown gave the Maple Leafs a 5-4 lead at 15:15.
Frederik Andersen made 36 saves for Toronto, which has 57 points, one behind the Bruins (26-23-6) for third place in the Atlantic Division with five games in hand.
David Pastrnak scored twice, and Torey Krug scored for the Bruins, who have lost two in a row after a three-game winning streak. Tuukka Rask allowed four goals on 14 shots before he was pulled at 10:17 of the second period. Zane McIntyre allowed two goals on 12 shots.
Video: TOR@BOS: Bergeron nets game-tying PPG on backhand
"You're always disappointed when you lose a game like that because it was a real important game for us and you lose it," Bruins coach Claude Julien said. "So you say, 'What do you take from it?' Well, I think I can still take the fact that we battled back ... so I think the guys, when they're down 4-1, they didn't pack their bags and leave. We fought back, so you like those kind of things, but at the end of the night it's about winning hockey games and I have to say that tonight we found a way to lose."
Pastrnak scored on the first shot of the game after a pass from Bergeron to give the Bruins a 1-0 lead at 2:17 of the first period. The Maple Leafs tied it 1-1 when Nylander scored at 11:15.
Van Riemsdyk turned a turnover into a goal at 8:31 of the second period for a 2-1 lead, and Nylander scored on a 2-on-1 38 seconds later.
Nylander completed his hat trick with a power-play goal from the right hash mark at 10:17 for a 4-1 lead. Pastrnak's second goal cut the lead to 4-2 at 10:55, and Krug's power-play goal made it 4-3 at 15:53.
Goal of the game
Maple Leafs forward Auston Matthews stripped Bruins forward David Backes at the Boston blue line and sent Nazem Kadri and Nylander into the zone on a 2-on-1. Kadri set up Nylander's first goal with a cross-ice pass.
Video: TOR@BOS: Matthews forces turnover, Nylander finds net
Save of the game
Andersen preserved the win with a kick save on a Brad Marchand wrist shot from the high slot with 12 seconds left.
Video: TOR@BOS: Andersen helps seal win with late kick save
Highlight of the game
Van Riemsdyk skated out from behind Boston's net to the top of the right circle and beat McIntyre with a wrist shot through a Mitchell Marner screen.
Video: TOR@BOS: JVR nets late go-ahead goal through traffic
Unsung performance of the game
Maple Leafs defenseman Jake Gardiner was plus-3 in a Toronto-high 25:41 of ice time. He had four hits and two blocked shots.
They said it
"We've just got to bring Pastrnak on the road with us all the time, take him to every rink. [Nylander] always wants to be better than No. 88, so we've just got to get a No. 88 on every team and he'll be unbelievable." -- Maple Leafs coach Mike Babcock on forward William Nylander, who has been friends with Bruins forward David Pastrnak since they were teammates with Sodertalje SK in Sweden
"We couldn't get it done even though we came [back] so many times. There's always one mistake, they got a goal. So it's too bad and we can't hang our heads. We have to learn from this and be ready for [the] next game." -- Bruins forward David Pastrnak
Need to know
Nylander has 13 goals this season; he played his 71st NHL game. … Tyler Bozak got his 300th NHL point with an assist on van Riemsdyk's game-winning goal. … Van Riemsdyk played his 300th game for the Maple Leafs. ... Bergeron had three points to extend his point streak to five games (three goals, five assists). ... Kadri had an assist and has 16 points (seven goals) in his past 14 games. ... Bruins defenseman Zdeno Chara matched his NHL high with three assists.
Maple Leafs: At the New York Islanders on Monday (7 p.m. ET; SN, MSG+2, NHL.TV)
Bruins: Host the San Jose Sharks on Thursday (7 p.m. ET; SN360, SNW, TVA Sports, NESN, CSN-CA, NHL.TV)
Video: Nylander, van Riemsdyk power Maple Leafs past Bruins