BOSTON — Mookie Betts said it was his greatest moment in baseball. David Price told Hanley Ramirez the same thing.

"It's an honour," Ramirez said after hitting a three-run homer with two outs in the ninth inning to finish off a five-run rally that gave the Boston Red Sox a 7-5 victory over the New York Yankees on Thursday night.

"Unbelievable comeback," the Boston first baseman added 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. "

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 Dellin 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 then sent the next pitch to straightaway centre field.

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.

"Our games with the Yankees are unbelievable games," said Ortiz, the only player left on the roster from the team that rallied from a three-game deficit in the 2004 ALCS against New York and went on to win the franchise's first World Series title in 86 years.

"This game was pretty much on their side," Ortiz added. "It's huge, man. Hanley is a guy that has been doing some unbelievable things. Look at the year that he's having. Can't ask for more than that. He's coming in every day to do something to make things happen."

The Red Sox moved two games ahead of Baltimore, which lost 7-6 to Tampa Bay. Instead of improving to three games back in the division, the Yankees face a five-game deficit after losing for the fourth time in five games.

They are three behind Baltimore and Toronto in the wild-card race, with Detroit and Seattle both in between.

"This one hurts," 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."

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.

Ortiz had three RBIs and three hits — two for extra bases to give him 1,186 in his career. That tied the slugger with Frank Robinson for 11th on the all-time list for extra-base hits.

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. 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. Also, right-hander Nathan Eovaldi was transferred to the 60-day DL to make roster room for Butler.

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 that 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).