BOSTON — The New York Yankees were one out from cutting their deficit in the AL East to three games — as close as they have been since April.

But then Dellin Betances couldn’t protect what was left of a four-run lead.

The closer gave up Hanley Ramirez’s three-run homer with two outs in the ninth Thursday night and the Red Sox scored five times in the inning to win 7-5.

Instead of trailing rival Boston by three games, the Yankees are five back and struggling to stay afloat in the wild-card race with four losses in five games.

“It’s frustrating,” said Betances, who lost for the second straight day. “We had that game. You don’t want to do that this late. Obviously, we’re trying to win games. When that happens, you’re not helping.”

New York took a four-run lead in the third inning and still led 5-1 before David Ortiz hit a solo shot in the eighth. It was the 537th home run of his career, passing Yankees Hall of Famer Mickey Mantle for 17th on baseball’s all-time list.

Ortiz also had an RBI single off Betances (3-6) in the ninth to make it 5-3. There were still two on and two outs when Ramirez worked a 3-1 count and sent the next pitch to straightaway center field.

New York is three games back of wild-card leaders Baltimore and Toronto, and a game behind Detroit and Seattle.

“This one hurts,” Yankees manager Joe Girardi said. “We’ve got to bounce back tomorrow. We were in a pretty good position going into the ninth inning and we weren’t able to close the deal.”

It was the sixth game-ending hit of Ramirez’s career and his first for Boston, earning him an ice-water drenching in front of the dugout as the crowd celebrated.

“Unbelievable comeback,” Ramirez said after helping the Red Sox snap a two-game skid and open a two-game lead in the AL East — knocking their archrivals back in the playoff race at the same time. “This one was a moment I really enjoyed. “

The Red Sox moved two games ahead of Baltimore, which lost 7-6 to Tampa Bay.

Starlin Castro had four hits and new addition Billy Butler drove in a pair of runs in his Yankees debut.

Joe Kelly (3-0) pitched into and out of a bases-loaded, one-out jam in the top of the ninth, striking out Chase Headley and then getting Mason Williams on a line drive back to the mound.

FOR STARTERS

Red Sox lefty Eduardo Rodriguez had experienced some bad luck since the All-Star break, winning only one of 10 starts despite a 2.76 ERA. But this time he made his own trouble, allowing four earned runs on eight hits and a hit batter in just 2 1/3 innings. He has four losses and six no-decisions in his last 10 starts.

Yankees starter Masahiro Tanaka allowed one run on four hits and three walks, striking out no one but leaving after seven innings with a 5-1 lead.

STUCK DOOR

The game was delayed heading into the top of the third when a door in the left-field corner was stuck. It was fixed after a couple of minutes.

COLLAPSE

Girardi tried to avoid using Betances in the ninth, starting with Tommy Layne for one out and then turning to Blake Parker, who nipped pinch-hitter Chris Young on the top of the helmet with a pitch.

Betances, the loser in Wednesday’s game against the Dodgers, walked Dustin Pedroia but got the second out when Young tried to score from third on a short grounder and was cut down at home.

With the crowd chanting “Papi! Papi!” Ortiz singled to make it 5-3. Mookie Betts singled to make it 5-4, and Gary Sanchez’s passed ball put runners on second and third before Ramirez ended it.

“The fastball that went out was 99 (mph),” Girardi said. “Everyone’s tired this time of year. That’s the thing. It’s the end of the year. We’re fighting for our lives, he’s our best guy and I went to him.”

TRAINER’S ROOM

Yankees: Butler was signed two days after rookie RF Aaron Judge hurt himself taking a swing. He has been put on the 15-day disabled list.

Red Sox: Manager John Farrell said he expected Ortiz to start each of the remaining 17 games this season. Ortiz has played in 134 of the first 146 games — starting 125 — taking time off along the way to stay healthy at 40 years old. He has not played in 151 games since 2006, and has topped the mark only once in his career.

UP NEXT

Yankees: RHP Luis Cessa (4-1) will start Friday night in Boston.

Red Sox: Boston goes with righty Clay Buchholz (6-10).