BOSTON — Each loss at this time of year takes on the narrative of “there’s no way they’ll come back from this one.”

Much of the time, that’s hyperbole, but what happened Thursday night at Fenway Park likely feels that way to Yankees fans, and maybe even the team itself.

After Masahiro Tanaka continued his recent brilliance, the bullpen couldn’t close the deal, allowing five runs in the ninth inning of a stunning 7-5 loss to the Red Sox in front of a delirious crowd of 37,767 — many of whom already had left when Hanley Ramirez hit a walk-off three-run homer. “A lot of guys are [ticked] off,” Chase Headley said, “because it’s a game you felt like you had in hand.”

With Baltimore and Detroit already having lost, the Yankees (77-69) missed an opportunity to gain ground. Instead of moving within three games of AL East-leading Boston with six games left between the teams, they fell five behind. The Yankees are three games behind Toronto and Baltimore, which are tied for the two wild-card spots.

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

On a 3-and-1 pitch, Ramirez hit a 98-mph fastball from Dellin Betances into the centerfield stands. Ramirez (25 homers) knew it was gone off the bat, pausing to admire it. “We had a good game and I wasn’t able to seal the deal,” Betances said.

advertisement | advertise on newsday

The Yankees were angry that Betances didn’t get a check-swing call on the preceding 2-and-1 pitch. The count went to 3-and-1 when first-base umpire D.J. Reyburn said Ramirez didn’t swing. “I definitely thought he went. Obviously, it changes the situation,” said Betances, who has allowed runs in four of his last five outings. “But at the end of the day, I have to make a [better] pitch, and I didn’t.”

Girardi hoped to stay away from Betances, who had pitched in the previous two games. Tommy Layne struck out pinch hitter Aaron Hill, but Blake Parker hit pinch hitter Chris Young with a pitch and Betances walked Dustin Pedroia.

With runners on second and third after defensive indifference, Xander Bogaerts grounded back to the mound, and Young was retired after a short rundown for the second out. Ortiz’s RBI single to center and Mookie Betts’ RBI single to left made it 5-4 and brought Ramirez to the plate with runners on first and second. After a passed ball put the winning run on second, Ramirez ended it.

Betances has said he tired near the end of last season, when he made a career-high 74 appearances, and this marked his 69th outing. He didn’t use that as an alibi, and Girardi wasn’t much interested in exploring questions about fatigue. “Everyone’s tired this time of year,” he said. “We’re fighting for our lives. He’s our best guy and I went to him.”

The Yankees, who led 5-1 at one point, had 14 hits — four by Starlin Castro and two each by Jacoby Ellsbury, Gary Sanchez, Didi Gregorius and Headley — but stranded 12 runners and failed to take advantage of several excellent opportunities to break open the game.

Yankees videos

They got a runner to third with less than two outs in the third, fifth and ninth innings — doing so with none out on the latter two occasions — but failed to score.

“If we would have done a little bit better job in the game picking up runs, he’s not even in the game,” Headley said of Betances. “Don’t put this one on Dellin. We had a chance to blow it open and we didn’t do it.”

Tanaka, who came in 13-4, was 4-0 with a 1.35 ERA in his previous five starts and 6-0 with a 1.94 ERA in his previous seven. He allowed one run and four hits in seven innings in lowering his ERA to an AL-best 2.97.

Adam Warren gave up Ortiz’s 34th homer in the eighth. It was Ortiz’s 537th homer, breaking a tie with Mickey Mantle.

Boston lefthander Eduardo Rodriguez, who entered the game with a 2.76 ERA in 10 post-All-Star break starts and had allowed a total of two earned runs in two previous starts against the Yankees this season, did not resemble the pitcher who put up any of those numbers. He allowed four runs and eight hits in 2 1⁄3 innings.

advertisement | advertise on newsday

The Yankees got after Rodriguez in the first. Ellsbury lined a single to center and Sanchez lined a 1-and-2 fastball over the head of centerfielder Jackie Bradley Jr. for a ground-rule double. Castro’s RBI single and Billy Butler’s sacrifice fly made it 2-0.

Castro doubled off the Green Monster with one out in the third and Butler sent a sharp RBI single to center. Gregorius doubled and Headley dumped an RBI single down the rightfield line to make it 4-0 and put an end to Rodriguez’s night.

Castro’s two-out RBI infield single in the fourth made it 5-1.

With runners on first and third and one out in the third, Rob Refsnyder struck out and Tyler Austin grounded out. With a runner on third and none out in the fifth, Headley, Refsnyder and Austin struck out against Junichi Tazawa. With runners on first and third and none out in the ninth against Joe Kelly, Brian McCann struck out, and after Gregorius walked to load the bases, Headley struck out and Mason Williams lined out to Kelly.