Pavel Zacha had a goal and two assists, and David Krejci and Mike Reilly each had a goal and an assist for the Bruins. Jeremy Swayman made 28 saves.
Mika Zibanejad and Kaapo Kakko each scored twice, and Jaroslav Halak made 21 saves for the Rangers.
Halak started in place of Igor Shesterkin, who was scheduled to play the entire game but sat out with a stomach issue.
Zibanejad gave New York a 1-0 lead 27 seconds into the first period when he scored from a sharp angle off Chris Kreider's pass across the crease.
Jake DeBrusk tied it 1-1 at 2:27 after getting to a loose puck near the top of the crease.
Zibanejad put the Rangers back in front 2-1 on the power play with a redirection in the slot at 18:08, and Kakko extended the lead to 3-1 on a breakaway with 45 seconds left.
Zacha cut it to 3-2 on a breakaway at 36 seconds of the second period after stealing the puck in the neutral zone.
Pastrnak tied it 3-3 at 8:09 on a one-timer from low in the left face-off circle, then fed Krejci for a go-ahead goal on the rush at 15:46.
Kakko tied it 4-4 at 6:49 of the third period when he knocked the puck across the goal line after Libor Hajek's shot trickled past Swayman.
Reilly scored at 12:10 with a shot from the right circle for the 5-4 final.