NEW YORK — Less than three months ago, Boston starter Doug Fister was standing on a minor league mound with an uncertain future. On Friday night, he hurled a gem against his club’s closest competitor in the middle of a pennant race.

Fister dominated for seven innings and the Red Sox backed him with three home runs to beat the New York Yankees 4-1 and extend their AL East lead.

Eduardo Nunez, Andrew Benintendi and Hanley Ramirez each homered as the Red Sox opened a 5 1/2-game edge over the Yankees.

Fister (4-7) gave up doubles by Brett Gardner and Aaron Judge to begin the first inning, and little else. He allowed four hits, struck out five and walked one.

“The veteran presence that he has, a guy that doesn’t take anything for granted,” Boston manager John Farrell said. “Kind of got a second life here this year after being in Triple-A for pretty much the first half of the season, but he has taken this opportunity and run with it and he’s pitching with a heck of a lot of conviction.”

On Aug. 22, facing AL Central-leading Cleveland, Fister gave up a leadoff home run to Francisco Lindor and then finished with a complete-game one-hitter.

“I am so grateful for the opportunity that I’ve had throughout my career,” Fister said, pitching with his fifth team in nine years. “But to start this season late and to kind of go from where I was to where I am, it’s an honor for me to be a part of this club.”

Fister mixed speeds well all night, getting ahead in the count often and keeping the Yankees off balance. In his last three starts, he has a 1.57 ERA.

“He pitches with such conviction and you see it in his body language, you see it in his attack plan,” Farrell said. “He’s got a clear understanding of what he’s trying to do on the mound.”

Craig Kimbrel pitched a perfect ninth to notch his 32nd save in 36 chances.

Nunez hit a two-run drive off Sonny Gray (8-9) for a 2-1 lead in the third. A day earlier, Nunez dropped down a bunt that Yankees pitcher CC Sabathia threw away, prompting the burly left-hander to call the Boston strategy “weak.”

Brock Holt walked on a full-count pitch in the third that appeared to be outside of the strike zone, one of many close calls that caused Gray to become visibly frustrated.

When asked about the pitch after the game, Gray mentioned that he hadn’t looked at it yet but later added: “I don’t know why he didn’t swing. He must’ve been taking all the way.”

Nunez followed by lining a fastball over the wall in left for his combined 12th homer of the season with San Francisco and Boston.

“That’s why they got me,” Nunez said. “To do everything that I can to help the team.”

With two outs in the fifth, Benintendi hit Gray’s first offering into the right field seats for his 19th home run. Benintendi’s 14 RBIs this season are the most against the Yankees by any rookie since Jose Canseco also drove in 14 in 1986.

“The field kind of plays to my swing. I feel like you see the ball well here,” Benintendi said. “The lighting’s good. The backdrop’s good.”

Ramirez connected in the seventh for his 22nd homer and third in four games.

“Hanley’s in that stretch where he makes such an impact on our offense as a whole,” Farrell said.

Gray had not allowed multiple homers in a game since his first start of the year. He struck out nine over seven innings.

Judge’s RBI was his first since Aug. 23.

HALL OF FAME COMPANY

Benintendi became the second Red Sox batter to hit five homers at Yankee Stadium in a single season. He joined Jim Rice, who did it in 1983.

BERN, BABY, BERN

Former Yankees star Bernie Williams performed the national anthem, playing it on his guitar as part of a pregame ceremony to raise awareness for idiopathic pulmonary fibrosis, a rare lung disease that claimed the life of his father.

Williams sat on a stool near the on-deck circle earlier in the afternoon, practicing his licks and chatting with former teammate and Red Sox hitting coach Chili Davis.

SEPTEMBER CALL-UPS

Both teams made a flurry of moves as roster limits expanded. Boston recalled catcher Blake Swihart, first baseman Sam Travis, right-hander Austin Maddox and left-hander Roenis Elias from Triple-A Pawtucket.

The Yankees added right-handers Ben Heller and Bryan Mitchell from Triple-A. Lefty Jordan Montgomery, who started on Wednesday against Cleveland, returned as well.

TRAINER’S ROOM

Red Sox: 2B Dustin Pedroia (left knee inflammation) was activated from the 10-day disabled list prior to the game. He singled in four at-bats and made a nifty diving stop to rob Jacoby Ellsbury of a hit. The former AL MVP played just one game in August. He is not expected to play Saturday. … RHP Matt Barnes (low back strain) also came off the DL. … CF Jackie Bradley Jr. (sprained left thumb) is expected back on Saturday. … LHP David Price (left elbow inflammation) worked a 29-pitch bullpen on Friday, throwing fastballs and changeups in a session that manager John Farrell called impressive and powerful. Price may throw another bullpen session on Sunday or Monday which could include breaking balls.

Yankees: DH Matt Holliday (back) was reinstated from the 10-day disabled list but did not play. … RHP Luis Cessa (ribcage injury) was transferred to the 60-day DL.

UP NEXT

Red Sox: LHP Drew Pomeranz (14-4, 3.23 ERA) has won a career-high eight consecutive decisions. He is 2-0 with a 3.43 ERA in four starts against New York this season.

Yankees: RHP Masahiro Tanaka (10-10, 4.69 ERA) has won both starts since returning from the DL, including a 10-strikeout performance against Seattle his last time out.