BOSTON — Starlin Castro’s two-run double in the seventh inning capped a five-run frame for the New York Yankees in a 9-4 win over the Boston Red Sox on Wednesday night at Fenway Park.

Didi Gregorius and Gary Sanchez each slugged solo home runs while Austin Romine, Jacoby Ellsbury and Chase Headley each added RBIs for the Yankees (57-56), who avoided falling below .500 for the first time since July 17.

Alex Rodriguez was greeted with boos from the Fenway fans when he entered as a pinch-hitter for Aaron Hicks with two on and nobody out in the seventh, but flied out to right field.

The 41-year-old Rodriguez, who will play his final game with the Yankees on Friday in New York before retiring, is scheduled to start Thursday’s series finale.

Yankees starter Nathan Eovaldi departed with right elbow discomfort prior to the top of the second inning after throwing only 12 pitches in the first. The hard-throwing right-hander was sent back to New York for further evaluation.

It was the shortest outing of Eovaldi’s career, tossing one fewer pitch than he did in a Sept. 12, 2011, relief appearance with the Los Angeles Dodgers during his rookie season.

Tyler Clippard (1-0), the Yankees’ sixth of eight pitchers, earned the win with a scoreless inning of relief. Dellin Betances pitched the ninth.

Mookie Betts, Andrew Benintendi and Dustin Pedroia each drove in a run for the Red Sox (61-51).

Drew Pomeranz fell short in his fifth attempt to notch his first win with the Red Sox, allowing Gregorius’ 14th homer of the year and five more hits with a walk and five strikeouts in 5 1/3 innings.

The first-time All-Star lefthander is 0-2 with three no-decisions and a 5.26 ERA after allowing 15 runs over 25 2/3 innings since being acquired in a July 14 trade from San Diego.

Fernando Abad (1-6), another Boston trade-deadline acquisition, took the loss after surrendering Castro’s double.

The Red Sox scored twice in the sixth on Pedroia’s single and Xander Bogaerts’ groundout to build a 4-1 lead, but the Yankees answered with their big fifth.

Romine and Ellsbury slapped RBI singles to bring New York within a run and Headley tied it with his single. Sanchez slugged his first career homer to straightaway center in the eighth and both Romine and Rob Refsnyder scored on wild pitches to pad the lead.

Betts left the game before the top of the eighth with right calf tightness and was replaced by Brock Holt in right field.

David Ortiz fouled a ball off his right knee in the ninth inning and fell to the ground in pain before being helped off the field. Bryce Brentz pinch-hit for Ortiz.

Chasen Shreve relieved Eovaldi and posted a scoreless second, but surrendered a run on Betts’ third-inning groundout.

Blake Parker came on after Shreve, but suffered a similar fate on Benintendi’s bases-loaded groundout in the fourth.

Gregorius slashed the deficit in half with his solo homer into Boston’s bullpen in right field to lead off the fifth.

NOTES: Boston RHP Steven Wright will miss Thursday’s scheduled start due to right shoulder inflammation sustained while sliding as a pinch-runner last Sunday. LHP Eduardo Rodriguez will start Thursday, LHP David Price will start Friday and RHP Clay Buchholz will return to the rotation Saturday. … Red Sox 2B Dustin Pedroia hit leadoff for the first time this season, while regular leadoff hitter RF Mookie Betts batted third for the second time ever. … New York manager Joe Girardi defended his decision not to start Alex Rodriguez in the first two games of the series in Boston. “My job description does not entail farewell tours,” Girardi said Wednesday. … Yankees RHP Michael Pineda (6-10, 5.17 ERA) opposes Eduardo Rodriguez (2-5, 5.93 ERA) in Thursday’s series finale.