It wasn’t any one hit that sparked what had been a snoozing Yankees offense.

It was Jacoby Ellsbury’s legs.

The centerfielder, who did not start Friday night’s game but was inserted after Aaron Hicks hurt his left shoulder, scored the tying run by stealing home on a 3-and-2 pitch with two outs in the fifth inning in a 6-3 victory over the Rays at the Stadium.

Ellsbury’s daring theft of home, which tied the score at 3-3, set things up for Brian McCann, who lined a tiebreaking RBI single to left off lefthander Matt Moore in the sixth. Ellsbury lined a two-out, two-run double in the eighth to make it 6-3.

advertisement | advertise on newsday

McCann’s two-run homer in the second had tied it at 2-2.

The victory allowed the Yankees (6-9), who had lost three straight and seven of eight, to breathe a sigh of relief.

They trailed 3-2 entering the fifth, and with two outs, Ellsbury and Didi Gregorius singled to bring up Brett Gardner. Both runners advanced on a balk by Moore, and the count eventually ran to 3-and-2.

Moore went into a full windup, never glancing at Ellsbury, who took off for the plate. The pitch came in high and Ellsbury — who stole home against the Yankees’ Andy Pettitte in April 2009 when he was with the Red Sox — slid across the plate just ahead of Curt Casali’s tag. “I knew if I took off, I was going to make it,” he said.

Said McCann, “I’m still stunned by what happened. I didn’t think I’d ever see that with two strikes. Great play.”

Yankees videos

Ellsbury said third-base coach Joe Espada talked to him just before the pitch. “He just said basically, ‘Watch him in the windup here.’ To me, that means one thing: I’m going.”

Said Joe Girardi: “We were trying to get his attention at 3-1. we were a little shocked he did it 3-2. Tremendous job by Gardy. I think it shocked Gardy . . . Obviously, there’s some things that could have gone wrong.”

Said Gardner: “At first, I thought he was just messing with him a little bit, trying to get him to balk. But then I realized he was doing more than just messing with the pitcher. Yeah, it was a surprise, but it worked out for the best. Heads-up play on his part. Thank goodness the ball wasn’t close to the zone.’’

Derek Jeter had been the last Yankee to record a straight steal of home on May 5, 2001.

Said Ellsbury: “Guys get fired up. It doesn’t happen very often. Guys couldn’t believe that happened . . . It’s exciting. That’s the ultimate adrenaline rush for a base-stealer.”

advertisement | advertise on newsday

“I felt like it changed a little bit of momentum,’’ Carlos Beltran said. “You just hope when a guy does that, it goes right. It’s a huge play. It’s a risky play, but if it goes well, it’s gratifying.”

Winning pitcher Ivan Nova, who took over for CC Sabathia with two outs in the fifth, threw 2 1⁄3 hitless innings. Dellin Betances struck out the side in an 11-pitch eighth and Andrew Miller struck out two in the ninth to pick up his fourth save. The three retired 13 of the 14 batters they faced, with the only runner reaching on an error.

Betances and Miller have struck out 35 in 15 innings and have not allowed an earned run.

After Logan Forsythe’s two-out, two-run double off CC Sabathia in the second, Mark Teixeira started the bottom of the inning with a sharp single to left, the first of his three hits. One out later, McCann launched a belt-high 95-mph fastball into the rightfield seats to tie it at 2-2.

With two outs in the fourth, Forsythe doubled to left-center, and when Brandon Guyer flared one to short left-center, a diving Hicks couldn’t quite get to it. He later left with what the Yankees called a “sore left shoulder.’’ He is day-to-day.