BOSTON — This time it was the Red Sox bullpen that blew a win. The Yankees know the feeling, and they certainly were happy to be on the other side of it.

Three outs from losing for the 20th time in their last 27 games, the Yankees got a tying homer from Matt Holliday off Craig Kimbrel to lead off the ninth.

Seven innings later, they pushed across three runs against Doug Fister and beat Boston, 4-1, in 16 innings in front of 36,936 at Fenway Park.

In 30 previous outings at Fenway Park, Kimbrel had not allowed a run. About half of that crowd remained by the end of the 5-hour 50-minute game, in which each team used eight pitchers. A total of 512 pitches were thrown.

How badly did the Yankees need this one?

“A lot,” said Didi Gregorius, who had the go-ahead hit.

advertisement | advertise on newsday

Joe Girardi called it “a huge win for this team the way things had been going.”

If you missed it, things had not been going well for the Yankees (46-42), who suffered their 18th blown save of the season the night before and are at the start of an 11-game road trip. They will play a split doubleheader with the Red Sox (51-40) today.

“Any time you can tie the game against a great closer like that,” Holliday said, “that’s one of the reasons you play the game.”

The Yankees’ bullpen, a significant factor in the club’s recent struggles, was terrific, combining to throw nine scoreless innings after an excellent start by Luis Severino.

Ben Heller picked up his second career victory after pitching the 15th and 16th. Jonathan Holder pitched the 11th, 12th and 13th and Aroldis Chapman, who blew a save and took the loss Friday night, worked the 14th.

Yankees videos

With the Yankees needing reinforcements for the doubleheader, Heller and Holder likely are headed back to Triple-A as a reward. “In the minor leagues, I pitched in a lot of close games,” Heller said. “But it’s kind of tough to simulate the adrenaline that you get in a game like this.”

Jacoby Ellsbury, who entered the game as a pinch runner in the ninth, led off the 16th with a double off the Green Monster in left. Chase Headley followed with a single and Didi Gregorius lined an RBI single to center to make it 2-1. Austin Romine’s RBI single made it 3-1 and Gary Sanchez lifted a long sacrifice fly to left for a 4-1 lead.

“I had been struggling with guys in scoring position lately,” Gregorius said. “I finally came through today.”

It had been mostly frustration at the plate for both teams until the 16th. The Red Sox went 1-for-10 with runners in scoring position, falling to 2-for-40 in such situations against the Yankees this season. The Yankees stranded 14 runners and the Red Sox left nine on base.

Both starting pitchers were excellent, though by game’s end, their contributions mostly had been forgotten.

advertisement | advertise on newsday

Chris Sale, the American League starter in the All-Star Game, brought an 11-4 record and 2.75 ERA into the game and was far better than those numbers, allowing three hits and striking out 13 in 7 2⁄3 innings. Sale recorded his 12th double-digit strikeout game in 19 starts.

Severino, who threw seven shutout innings at the Red Sox here April 26 and also made the All-Star team, again was terrific, allowing one run and four hits in seven innings. He retired the final 11 batters he faced, walked two and struck out six in lowering his ERA to 3.40.

“We needed a big day from him,” Girardi said. “He knew he couldn’t give up much with Chris Sale on the mound and he kind of went toe-to-toe with him.”

One of the oddest innings in a game full of them was the 11th. Holliday worked a leadoff walk against Heath Hembree and Farrell brought on Robby Scott to face Ellsbury, who sent a grounder to first. Mitch Moreland threw to Xander Bogaerts for the forceout, but as Holliday went sliding back into first — apparently thinking Moreland had stepped on first base and that a rundown was in progress — Bogaerts’ relay, which would have been in time to retire Ellsbury, hit Holliday and couldn’t be caught by Moreland.

The Red Sox wanted Holliday called for interference and — after a delay of nearly 10 minutes, which included two umpire conferences with replay central in New York — they indicated their intention to play the remainder of the game under protest. Scott retired the next two batters to end the inning.

advertisement | advertise on newsday