NEW YORK — J.D. Martinez hit a tie-breaking home run against Dellin Betances leading off the eighth inning, just beyond Aaron Judge’s reach at the right-field wall, and the Boston Red Sox rebounded after wasting a four-run lead to beat New York 5-4 Thursday night and send the Yankees to only their second loss in 19 games.

In another dramatic game before a sellout crowd, Boston moved back into a tie with the Yankees for the AL East lead at 26-11, the best record in the major leagues. The rivals have 13 more games this year but don’t meet again until June 29 in the Bronx.

Boston built a 4-0 lead against CC Sabathia as Hanley Ramirez drove in three runs with an RBI groundout in the first, an RBI single in a two-run third and a solo homer in the fifth just before a 55-minute rain delay.

But the Yankees tied the score in the seventh after loading the bases with one out against Heath Hembree.

Joe Kelly (2-0), who served a six-game suspension for hitting the Yankees’ Tyler Austin with a pitch last month, was booed loudly when he entered. Kelly forced in a run with a four-pitch walk to Brett Gardner, gave up an RBI single to Judge and a run-scoring groundout to slumping Didi Gregorius, then bounced a wild pitch off catcher Christian Vazquez as Gardner scored standing up.

Martinez, who drove in a run in the third with a liner off the glove of second baseman Gleyber Torres that was turned into a forceout, sent a 97 mph fastball from Betances (1-2) to the opposite field, just over Judge’s outstretched glove. Judge reached over the fence and looked for a fan interference call when a spectator’s glove touched his, but the ball was clearly over the wall and Martinez circled the bases with his ninth home run.

Austin flied out on the first pitch he saw from Kelly with a runner on in the eighth. Kelly threw a called third strike past Neil Walker to end the inning with two on – the seventh Yankees batter caught looking as New York repeatedly questioned the strike zone of plate umpire Stu Scheurwater.

A night after giving up Gardner’s tie-breaking, two-run triple in a four-run eighth, Craig Kimbrel struck out Gardner, then retired Judge on a flyout and Gregorius on a chopper that Kimbrel snagged on the left side of the infield for his 10th save in 12 chances.

Mookie Betts had three hits, raising his batting average to a major league-leading .361.

New York had won eight straight games overall and 11 in a row at home. Boston improved to 11-1 in series finales this season and is unbeaten in Eduardo Rodriguez’s seven starts.

Rodriguez allowed just one hit in five scoreless innings, a fourth-inning smash by Giancarlo Stanton off the glove of third baseman Rafael Devers. Down 3-0, New York loaded the bases with one out before Rodriguez threw a called third strike past Austin and retired Miguel Andujar on a flyout to the front of the warning track in right-center.

Rodriguez struck out eight – including six looking. He has a 2.25 ERA in six starts at new Yankee Stadium, third-best among opposing pitchers with at least five starts there behind Felix Hernandez (1.41) and Chris Sale (2.18).

Sabathia had the worst of his seven outings this year, allowing nine hits in four innings as his ERA rose from 1.39 to 2.23.