BOSTON — Alex Rodriguez got the raucous booing he expected and the victory his team needed.

Rodriguez drove in a run with a bases-loaded dribbler, capping a three-run rally in the eighth inning that lifted the New York Yankees over the Red Sox 4-2 on Thursday night and sent New York’s polarizing designated hitter out of Boston with one last victory.

“It was a lot of fun to be out there,” Rodriguez said after his only start in the series. “Kind of a funny hit at the end. Not how I pictured it when I woke up this morning, but definitely I’m glad that run came in and we won the game.”

Rodriguez is set to play his final game for New York on Friday night at Yankee Stadium against Tampa Bay. The slumping 41-year-old will then be released and become an adviser and instructor for the team.

Rodriguez was 0 for 4 and loudly jeered throughout the game, which turned in the Yankees’ favor on a crucial fielding mistake that allowed them to overcome a 2-1 deficit and take two out of three in the series.

Rodriguez lined out, popped out and struck out before his tapper in front of the plate in the eighth rolled maybe 10 feet – far enough for Brett Gardner to score from third and put New York up 4-2.

“That’s how I’m going to remember it – the RBI,” Rodriguez said. “I’m not going to tell anyone how it came.”

Rodriguez sat Tuesday in the opener and flied out as a pinch hitter in the Yankees’ 9-4 win Wednesday – his first at-bat in more than a week. Manager Joe Girardi fulfilled an earlier promise to put him in the lineup Thursday and said he wasn’t going to pinch hit for Rodriguez in the eighth.

“I really thought he might hit a ball in the seats tonight,” Girardi said. “But it ends up being a big run. It really does.”

The RBI for Rodriguez came after the Red Sox intentionally walked Chase Headley with one out. And it punctuated the frustration for Boston fans who had just seen former Red Sox star Jacoby Ellsbury, booed almost as loudly as Rodriguez, hit a fly ball to left field that rookie Andrew Benintendi misread with the bases loaded.

“It went in the lights, but that’s no excuse. I should have caught it,” Benintendi said. “It’s just one of those things. Not much you can do about it. I tried to put my glove up where I thought it was going to be.”

The ball went past Benintendi, allowing two runs to score on what was originally ruled a sacrifice fly and error. After the game, it was changed to a two-run double.

The Yankees’ other run came on Austin Romine’s solo homer to left in the third.

Dellin Betances earned his third save, striking out Dustin Pedroia and Xander Bogaerts with runners at the corners to end it.

Luis Cessa (2-0) pitched two scoreless innings. Brad Ziegler (0-3) got the loss.

Boston starter Eduardo Rodriguez gave up one run and three hits in seven innings.

Hanley Ramirez drove in both runs for Boston with a single in the first and an RBI double in the fifth off Michael Pineda.

Ramirez later was knocked down in a collision by Gary Sanchez while trying to field a bounced throw at first base. Ramirez was checked by a trainer and stayed in the game.

NOTES: Mookie Betts was out of the starting lineup after leaving Wednesday night’s game early with tightness in his right calf. Manager John Farrell said Betts’ calf was still sore and he needed a night off.