Boston chipped away at a 5-2 deficit and tied the game in the seventh inning, but DJ LeMahieu's RBI double to left scored Anthony Volpe, who beat out an infield single to begin New York's next at-bats.
Before the seventh, the Yankees (74-73) played with the lead after Aaron Judge's grand slam in the second.
After Estevan Florial was hit by a John Schreiber pitch with one out in the ninth, Oswald Peraza delivered key insurance for the Yankees in the form of a two-out, two-run homer to center.
For New York, which won three of four in the series, LeMahieu went 3-for-5 with two RBIs and a run scored, while Volpe and Peraza finished with two hits apiece.
Yankees starter Clarke Schmidt allowed four runs (three earned) in 5 1/3 innings. Tommy Kahnle struck out four over two hitless innings to save a win for reliever Wandy Peralta (4-2).
Rafael Devers went 3-for-3 with a solo homer, and Ceddanne Rafaela went 2-for-5 and scored twice to pace Boston (74-73). Mauricio Llovera (1-2) took the loss in a bullpen game.
The Red Sox took the lead three batters into the first as Rafaela and Devers knocked back-to-back singles before shortstop Volpe's throwing error on Alex Verdugo's fielder's choice allowed the opening run to score.
Later in the inning, Wilyer Abreu's line single to left with two outs made it 2-0.
It didn't take long for the Yankees to respond with a five-run second against Brennan Bernardino, who entered the game following Boston opener Nick Robertson.
New York's big inning began with a Florial base hit before Peraza doubled into the left-field corner. A walk to Oswaldo Cabrera loaded the bases, and LeMahieu — following a replay review — beat out an RBI infield single.
Then, Judge towered a grand slam into the bleachers in straight center.
Devers led off Boston's third with a solo homer into the bullpen, making it a 5-3 game.
Though the Red Sox got back into the game, a double-play ball in three straight innings from the second through fourth helped Schmidt along. The first two were inning-enders.
In the fifth, Volpe's second throwing error allowed Triston Casas to reach with two outs and scored Luis Urias, who lined a leadoff single to center.
With Peralta in the game following Rafaela's single to begin the seventh, Devers dropped a hit into shallow center and Verdugo hustled out of a would-be double play.
Casas' grounder to third allowed Rafaela to score Boston's tying run.
—Field Level Media