BOSTON — Hanley Ramirez knew the ball was gone, knew the Boston Red Sox had just won a very important baseball game.

“It was one of those moments. You gotta enjoy it and let everything go,” Ramirez said after his two-out, three-run homer capped a five-run, ninth-inning rally and gave the Red Sox a 7-5 dramatic victory over the New York Yankees in the opener of a four-game series Thursday night.

After getting a break on a 2-1 check swing that looked like a swing, Ramirez hit a 3-1 fastball from Dellin Betances into the center field bleachers, setting off a wild on-field celebration.

“It was big,” Ramirez said of the win that restored Boston’s two-game lead atop the American League East entering Friday night’s game. “After last night (a 1-0 loss to the Baltimore Orioles that cut the lead to one game), it was big. Everybody was like, ‘We gotta win tonight,’”

And they did because of Ramirez’s first walk-off hit in a Red Sox uniform, the sixth of his career. It was his fifth homer in his past eight games.

David Ortiz, who drove in three runs (one with his 34th homer), singled home one run off Betances (3-6), and Mookie Betts then singled in another before Ramirez hit his 25th homer of the season and reached the 100-RBI mark.

Betances, working for a third consecutive day, saw his 2016 Fenway Park nightmare continue. He has worked in five games at Fenway this season, allowing seven runs on seven hits, including three homers, in just four innings.

“We had a good game and there I wasn’t able to seal the deal,” he said during a lengthy postgame media session. “I just left it up. Obviously, I’m trying to see if I can beat (Ramirez) with the heater, and he was right on it. I made a bad pitch.”

Yankee manager Joe Girardi was clearly trying to stay away from his young closer, who picked up his fifth blown save in 16 chances.

“We said all along that if we get one guy on, we’re going to Dellin,” Girardi said.

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

The Yankees, who have surged into the wild-card chase, were an out away from moving within three games of the Red Sox in the division.

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

With Boston down 5-1, Ortiz hit a solo shot off Adam Warren in the eighth for the 537th home run of his career. He snapped a tie with Yankees great Mickey Mantle for 17th place on the all-time homer list. Ortiz was also in the middle of the ninth-inning rally, collecting his 114th RBI.

“I’m not really focused about any of these personal numbers,” he said. “I’m focused on winning. We need to win right now. We need to do whatever it takes to win. Because the race right now, it’s very close. You look at the standings, every win matters. It’s great to be part of history, but right now I’m not focusing on that. I’m focusing on winning.

“Our games with the Yankees are unbelievable games. Coming from behind against a guy like Betances is huge. Betances is super nasty. He’s got some of the best stuff in the game, so it’s big, pretty big.”

The late collapse cost Yankees starter Masahiro Tanaka what would have been his seventh straight win (in eight starts). He worked seven strong innings, allowing one run.

Starlin Castro had four hits and two RBIs, Didi Gregorius stroked a pair of doubles and stole third base, and Chase Headley had two singles, an RBI and a stolen base. However, the Yankees wasted enough chances to keep the game close.

The Yankees made short work of left-hander Eduardo Rodriguez, who lasted 2 1/3 innings in his 10th straight start without a win.

Joe Kelly (3-0) got the win with a scoreless inning in relief.

NOTES: San Diego GM A.J. Preller was suspended for 30 days by Major League Baseball for allegedly hiding medical information on Drew Pomeranz before trading him to Boston. … LF Andrew Benintendi, out since Aug. 24 with a sprained left knee, returned for Boston and went 1-for-3. … The Yankees announced they will honor the retiring David Ortiz prior to the Sept. 29 game against the Red Sox in the Bronx.