ST. PETERSBURG, Fla. — Gary Sanchez is doing all he can to keep the New York Yankees in playoff contention.

The rookie slugger hit his 17th homer in 42 games this season, a three-run shot in a four-run seventh inning that sent the Yankees past the Tampa Bay Rays 5-3 on Tuesday night to snap a five-game skid.

“Amazing, incredible, impressive,” teammate Mark Teixeira said. “He’s carried us. We’re not even close to talking about a playoff berth if Gary doesn’t come up and do what he does.”

The banged-up Yankees moved within 3 1/2 games of Baltimore for the second AL wild card. New York would also have to jump over the Mariners, Astros and Tigers to earn a postseason berth.

Sanchez tied Wally Berger of the 1930 Boston Braves for the most homers by a big league player through 44 career games, according to Baseball-Reference data available through 1913.

“I’m enjoying it, especially when you win the game,” Sanchez said through a translator.

Sanchez gave the Yankees a 5-2 lead with his drive off Brad Boxberger (4-3). It was the young catcher’s sixth home run in 11 games following a 10-game drought.

“He’s meant a lot,” New York manager Joe Girardi said. “Offensively and defensively. It’s kind of fitting that he got the big hit.”

Sanchez’s shot came after Tampa Bay opted not to walk him with first base open and two outs.

“Had Boxy fallen behind, maybe you’d think about it, but this is a guy who’s gotten a lot of big outs for us in the past and trust him to get outs,” Rays manager Kevin Cash said.

Teixeira hit a solo shot in the fourth for New York, which was swept in a four-game series at Boston last weekend and had lost seven of eight overall.

It was Teixeira’s 407th homer, which tied Duke Snider for 54th place on baseball’s career list. The switch-hitting first baseman has hit 204 for the Yankees, moving him past Roger Maris and into a tie with Robinson Cano for 14th in franchise history.

“I haven’t hit a ball right-handed like that in a long time,” Teixeira said.

Yankees center fielder Jacoby Ellsbury (bruised right knee), third baseman Chase Headley (back spasms) and second baseman Starlin Castro (strained right hamstring) were all out of the lineup for the second straight game.

Luis Severino (3-8) pitched 1 1/3 innings of scoreless relief to earn the win. Dellin Betances got three outs for his 12th save.

New York starter Michael Pineda gave up two runs and struck out 11 in 5 1/3 innings.

Brad Miller put the Rays up 2-0 with a two-run triple in the third.

Tampa Bay lefty Drew Smyly allowed one run over six innings.

TRAINER’S ROOM

Yankees: Ellsbury didn’t rule out starting Wednesday. … Headley could be back late this week. … OF Aaron Hicks (right hamstring) was reinstated from the 15-day disabled list.

Rays: OF Steven Souza Jr. (left hip) and 1B Logan Morrison (left wrist) will both have surgery Wednesday. … Utility man Nick Franklin (left hamstring) might not play again this season.

KAPRIELIAN’S COMEBACK

Yankees minor league right-hander James Kaprielian, out since late April with an elbow injury, threw 30 pitches during his second batting practice session. The 2015 first-round draft pick might pitch in the Arizona Fall League. “This year I learned a lot, and for me this is a big character test,” Kaprielian said. “Battle these frustrations. I think in the long run this is going to help me.”

UP NEXT

Yankees: RHP Masahiro Tanaka (13-4), who is 6-0 in his last eight starts, takes the ball Wednesday night. He is 5-0 in seven career starts against the Rays.

Rays: RHP Alex Cobb (1-0) makes his fourth start since returning from Tommy John surgery.