J.D. Martinez hits an eighth-inning homer that stands as the winning run in Boston's 5-4 triumph in New York.

NEW YORK -- It was a small measure of redemption for Joe Kelly and the first time J.D. Martinez has done game-changing damage against the Yankees in the Bronx during his Red Sox career.

The fiery reliever and the free agent slugger proved a brassy pair in the eighth inning with Thursday night’s game slipping away from Boston.

New York was gunning for the series sweep and an extension of its torrid recent run, its 11th come-from-behind victory one swing away. It was not to be, as the Red Sox finally struck back for a 5-4 win that silenced the Yankee Stadium crowd and made it a tie once again atop the American League East.

Martinez led off the top of the eighth against Yankees reliever Dellin Betances, with New York pulling into a 4-4 tie the previous half-inning and seemingly on its way to a ninth straight win. Martinez struck a high fly to right that slipped just beyond the leap of 6-foot-7 outfielder Aaron Judge, disappearing beyond the wall to account for the eventual winning run. Kelly and Craig Kimbrel scraped together the final six outs as Boston salvaged the finale in nail-biting fashion.

Kelly atoned for a host of rocky moments in the series by gutting through the bottom of the eighth, surviving a leadoff walk to Gary Sanchez and a two-out single by Gleyber Torres. He froze Neil Walker on a 2-2 changeup down in the zone, grinning mischievously and pumping his fist while striding back to the visiting dugout. It was a vastly different outcome than his previous outing Tuesday night, as Aaron Judge singled home the winning run against Kelly in a 3-2 Yankees’ victory.

For the third time in as many nights, the Red Sox bullpen looked shaky against its oldest rivals. New York rallied in the seventh while Boston’s seemingly comfortable lead burned, as Heath Hembree and Kelly combined to allow four runs on a single hard-hit ball. Nine straight pitches outside the strike zone during one stretch was the lowlight, and Kelly’s bounced breaking ball in the dirt brought in Brett Gardner from third base to make it 4-4.

Matt Barnes had been summoned to face the heart of the Yankees order in the sixth and emerged unscathed. The same couldn’t be said for Hembree, who retired the first man he faced in the seventh before Miguel Andujar’s single to right center and an excuse-me looper to right by Torres. Hembree then walked pinch hitter Neil Walker to load the bases, bringing Kelly on to a cascade of boos from the sellout crowd of 46,899.

Kelly proceeded to throw four straight balls of his own, walking Gardner, and Judge grounded an RBI single through the left side. Didi Gregorius sent a bouncer to second for an RBI fielder’s choice and the wild pitch made it a new game with two innings left.

C.C. Sabathia dominated the Red Sox while winning his last five starts against them, allowing just 19 hits and striking out 26 in 33.1 innings pitched. Boston was on the board after just three batters while flipping the script on Thursday, with Mookie Betts sending a ground-rule double to right and scoring on a pair of infield outs to make it 1-0.

The Red Sox were at it again in the third, with Betts figuring prominently yet again. His leadoff single to center and a double to right by Andrew Benintendi set the table with no outs, one cleared by a broken-bat infield single from Hanley Ramirez and an infield out by Martinez. It was a 3-0 lead for Boston, its largest since Sunday’s series finale at Texas.

Ramirez slashed a liner into the Red Sox bullpen in left center to lead off the fifth, a solo homer that made it 4-0. The ferocious drive seemed worthy of lighting up the night sky, and Mother Nature soon took care of that. A booming thunderstorm rolled through immediately following, causing a 55-minute delay.

Red Sox manager Alex Cora said Eduardo Rodriguez “hated” his last outing against the Rangers, a game that wound up a 6-5 Boston victory. Rodriguez allowed three home runs among his four hits, his final line a bloated one despite 10 strikeouts. Similar mistakes were avoided on Thursday, as Rodriguez gave up just an infield single to Giancarlo Stanton and struck out eight over five shutout innings.