BOSTON — The New York Yankees are losing games and players as they fade from contention.

A week ago, the Yankees had closed within three games of the AL East lead and were one game back for the AL’s second wild card.

Now they’re seven games back of first and four games back for a wild card with a little more than two weeks left. And second baseman Starlin Castro and center fielder Jacoby Ellsbury may be joining right fielder Aaron Judge on the sidelines.

Castro left Saturday’s 6-5 loss to the division-leading Boston Red Sox in the fifth inning after straining his right hamstring while rounding first base on a double. Ellsbury slid into a bullpen wall trying to make a catch and injured his right knee.

“It’s not what you want and, hopefully, they’re not too severe and we’re not without them for a while,” New York manager Joe Girardi said. “I’m pretty worried about Castro and that hamstring, the way he pulled up. Hopefully Jake’s is just a bruise and a couple of days off will help him.”

Both players were being sent back to New York for further tests.

New York (77-71) wasted 3-0 and 5-2 leads and has lost six of its last seven games, including four in a row. Rookie Gary Sanchez hit his 15th home run since his Aug. 3 recall from the minors.

Xander Bogaerts hit a two-run homer and drove in three runs, and Mookie Betts had a tying single before scoring the go-ahead run as the Red Sox posted their eighth win in 11 games. Hanley Ramirez and Bogaerts each had three hits for the Red Sox.

New York opened a 5-2 lead on Austin Romine’s two-run double off the Green Monster in the fourth. Chase Headley, the second baserunner on the play, read the flight of the ball well and was just a few feet behind Gregorius when the pair crossed home plate.

Bogaerts homered off a billboard above the left-field wall in the fifth, slicing the deficit to 5-4, and the Red Sox took a 6-5 lead in the seventh with two well-placed bounces.

Betts’ high-chop grounder bounced over drawn-in shortstop Didi Gregorius to score Bogaerts, and Betts scored when Adam Warren (3-2) bounced a wild pitch that caromed to the right of Sanchez. Ramirez, who started on second, went to third, stopped, then tried to score when he saw Sanchez had his back to the plate. The catcher scampered back in time for the tag.

“He seemed to kind of chop at it, so you get the groundball you want,” Warren said. “Just went over his head, that’s baseball and unfortunate.”

Matt Barnes (4-3) pitched 1 1/3 innings in relief of David Price, and Craig Kimbrel struck out the final four batters for his 27th save in 29 chances.

New York took a 3-0 in the third on Brett Gardner’s RBI triple and Sanchez’s homer, which cleared the Monster seats above the left-field wall.

TRAINER’S ROOM

Yankees: Ellsbury stayed in for an inning before leaving the game. Girardi said he tried to run but said he couldn’t.

NOT LONG ENOUGH

Yankees starter Bryan Mitchell was pulled after giving up four runs — three earned — in 4 2/3 innings. His last batter was Bogaerts, who homered.

“Our plan today was to go out and try to set the tone for the game and dictate it and stay aggressive,” he said. “I feel like we did a good job of that early on. The leadoff walk in the fifth is ultimately what cost me. Missed a spot on Bogaerts.”

UP NEXT

LHP CC Sabathia (8-12, 4.10 ERA) is to start Sunday night’s series finale for the Yankees. He has allowed three or fewer runs in six of his last seven starts. LHP Drew Pomeranz (2-5, 4.60) starts for Boston after allowing five runs over two innings Tuesday in a loss to Baltimore, his shortest start since he was acquired from San Diego in July.