New York Yankees' Rob Refsnyder celebrates scoring on a sacrifice...

New York Yankees' Rob Refsnyder celebrates scoring on a sacrifice fly by Jacoby Ellsbury during the sixth inning of a baseball game against the Detroit Tigers in Detroit, Thursday, June 2, 2016. Credit: AP / Paul Sancya

DETROIT — Rob Refsnyder to the rescue.

The Yankees, who were held hitless for five innings by a pitcher with a 6.85 career ERA, snapped out of their offensive funk and ended a three-game losing streak with a 5-4 victory over the Tigers in a makeup game Thursday night at Comerica Park.

Refsnyder, the infielder/outfielder who has been begging for a shot for two years now, got the Yankees’ first hit, scored their first run and drove in the go-ahead run. Refsnyder (2-for-3) was a surprise starter at second base as Joe Girardi shifted Starlin Castro to shortstop and rested Didi Gregorius.

“He had a big night for us,” Girardi said of Refsnyder, who has four hits in 10 at-bats this season for a team that is starved for offense.

Fans of the Yankees’ minor leagues have been clamoring for Refsnyder to get an opportunity. He made the most of this one. The Yankees will face three righthanders this weekend in Baltimore, so it will be interesting to see if Girardi finds a way to get Ref snyder, who bats righthanded, into the lineup.

“He’s going to play,” Girardi said. “I’m not looking at him to play every day now, but he’s going to play. He’s going to get his opportunities.”

Said Refsnyder: “I’m not in charge of that. I’m just kind of in charge of preparing. I’ve got some early work tomorrow. That’s all I’m focused on.”

The Yankees did not have a baserunner against lefthander Matt Boyd until Chase Headley walked with two outs in the fifth. Their first hit was Refsnyder’s double to left-center leading off the sixth in a 1-0 game. Refsnyder scored on Jacoby Ellsbury’s sacrifice fly.

After Alex Rodriguez was thrown out at third on Austin Romine’s sacrifice attempt, leaving runners on first and second with one out in the seventh, Refsnyder sparked a four-run uprising with an RBI single to left on Boyd’s first pitch to give the Yankees a 2-1 lead.

That was it for Boyd (0-1), who after giving up no hits in the first five innings allowed six in a 10-batter span of the sixth and seventh. The Yankees had eight hits in those two innings and were held hitless in the other seven. “You definitely know,” Refsnyder said. “You definitely notice the zero. There was no sense of panic or anything like that.”

Former Mets closer Bobby Parnell made his Tigers debut and gave up a run-scoring single to Aaron Hicks to make it 3-1. Lefthander Kyle Ryan then allowed a two-run triple into the rightfield corner by Ellsbury to give the Yankees a comfy-seeming 5-1 advantage.

But it wasn’t that comfy after all as Dellin Betances, Andrew Miller and Aroldis Chapman each gave up a run.

Yankees starter Michael Pineda allowed one run in 5 2⁄3 innings. Pineda, who lowered his ERA from 6.92 to 6.41, gave up seven hits (three to Miguel Ca brera) with no walks and struck out eight.

Betances (2-2) got the last out of the sixth before allowing a run in the seventh on a walk and an RBI double by Mike Aviles. Miller walked two and struck out two in the eighth before giving up an RBI double to left to pinch hitter Ian Kinsler.

Gregorius, who entered for defense in the inning, took the relay throw from Brett Gardner on the play and gunned down Justin Upton at the plate to end the inning with the Yankees leading 5-3.

Gregorius teamed with Castro to help snuff out a Tigers rally in the ninth. With the bases loaded and none out against Chapman, Gregorius ranged behind second on a ball hit by J.D. Martinez and started a miraculous 6-4-3 double play as a run scored. Castro caught Gregorius’ glove flip with his bare hand while semi-sliding into second and threw to first while blowing a bubble.

With the tying run at third, Chapman got Cabrera to ground out to second on his first pitch for his eighth save.

“They’ve been saving us the whole time,” Gregorius said of the Big Three. “They got runners on, but we got out of it. We got the win. That’s the only thing that’s important.”

The Yankees came from Toronto, where they were swept in a three-game series, for a makeup of an April 10 rainout. They left after the game to continue their 10-game trip in Baltimore with a spring in their step.

More Yankees headlines