BOSTON — Alex Rodriguez didn’t exactly go out with a bang, but he produced a key run in his final game at Fenway Park as a member of the New York Yankees on Thursday night.

Rodriguez was 0-for-4 with a strikeout, but his dribbler to the mound in his final at-bat drove in a run to cap a three-run Yankees eighth inning. New York came from behind to post a 4-2 win over the Boston Red Sox.

“Kind of a funny hit at the end,” Rodriguez said. “Not how I pictured it when I woke up this morning.”

Rodriguez, batting cleanup in his final road start, was vigorously booed as he stepped to the plate for the first time in the second inning.

It was a fitting send-off in the eyes of the 41-year-old slugger, who will be released after New York’s game against the Tampa Bay Rays at Yankee Stadium on Friday.

“They’re exactly what they’ve meant for a long time,” Rodriguez said about the boos. “They’re passionate fans. They love their team.”

“Just like in the ’80s when Celtics-Lakers was good for (basketball), Red Sox-Yankees is also good for baseball.”

Rodriguez proceeded to hit a light popout to second following a six-pitch at-bat in the second.

Prior to Rodriguez’s RBI groundout in the eighth, former Red Sox center fielder Jacoby Ellsbury hit a decisive two-run, line-drive double to left.

Red Sox rookie left fielder Andrew Benintendi misplayed what looked like a catchable ball, allowing it to roll to the wall.

The play was originally ruled a sacrifice fly for Ellsbury and a run-scoring error on Benintendi, but the decision was changed after the game.

“It went in the lights, but that’s no excuse,” Benintendi said. “I should have caught it.”

Austin Romine added a solo home run, his fourth of the year for New York, which won two of three games in Boston and improved to 6-4 in August.

Yankees starter Michael Pineda allowed two runs on eight hits and two walks with three strikeouts over six innings.

Luis Cessa (2-0) earned the win with two scoreless innings, and Dellin Betances survived a rocky ninth for his third save.

Despite a trade-deadline selloff, the Yankees (58-56) are now just 6 1/2 games behind the American League East-leading Toronto Blue Jays and 3 1/2 games out in the AL wild-card race.

“I’ve been preaching it, we’re fighting,” Yankees manager Joe Girardi said. “We’re fighting, we’re fighting, and we’re going to continue to fight.”

Hanley Ramirez drove in both runs for Boston, which holds a half-game lead on the Detroit Tigers for the second AL wild card.

The Red Sox (61-52) lost for the fourth time in five games and dropped below 10 games over .500 for the first time since July 8. They trail the Blue Jays by three games in the AL East.

Red Sox starter Eduardo Rodriguez lasted seven innings, giving up a run on three hits and a walk with six strikeouts.

Brad Ziegler (0-3) surrendered all three runs in the eighth.

“I felt like I made a couple pretty good pitches,” Ziegler said. “The one Ellsbury hit wasn’t even a strike.”

Sandy Leon’s leadoff double and Benintendi’s walk put runners on the corners for Dustin Pedroia with one out in the Boston ninth. However, Pedroia and Xander Bogaerts both struck out.