OAKLAND, Calif. — Even at their most recent early-season worst, eight games under .500 at 9-17 after a loss May 5, Joe Girardi figured there would be a turnaround.

“I always believed we were a lot better than we were playing. We just weren’t hitting,” Girardi said late Sunday morning. “You’ve seen it time and time again where hitting comes and goes and you’ve got to deal with it. I knew it would turn and I knew that we’d start scoring runs, and that’s what happened.”

It continued happening Sunday afternoon as the Yankees completed a four-game sweep and extended their season-best winning streak to five games with a 5-4 victory over the A’s.

Coming off a 7-3 homestand, the Yankees finished their road trip at 5-2. The 12-5 stretch, which followed a 5-15 slump, allowed them to inch within a game of .500 at 21-22.

“I know in here, no one panicked,” Brian McCann said. “We know what we have in here.”

Girardi said the vibe in the clubhouse is “a lot better” and added, “There was a lot of frustration in our clubhouse, whether it was from pitchers or position players that were struggling with the bat. Everyone seems to be doing their part now.”

advertisement | advertise on newsday

The Yankees had 10 hits, getting solo home runs from McCann and Jacoby Ellsbury, a solid six-inning outing from the previously struggling Michael Pineda and a dominant last three innings by the Blast Furnace Boys at the back end of the bullpen, Dellin Betances, Andrew Miller and Aroldis Chapman.

Betances, Miller and Chapman pitched together for the fifth time, though not without some theater.

After Betances struck out two in a perfect seventh to protect a 5-3 lead, Didi Gregorius and Starlin Castro committed back-to-back errors on grounders to open the eighth. Miller struck out cleanup man Danny Valencia, but pinch hitter Billy Butler’s grounder to third made it 5-4. Miller got pinch hitter Khris Davis to ground to third to end the inning. Chapman struck out one in a perfect ninth for his sixth save in six chances.

The A’s (19-26) led 3-2 in the sixth inning before Brett Gardner scored from second on Mark Teixeira’s two-out infield single, which snapped an 0-for-19 skid. Teixeira hit a grounder to the right of second baseman Chris Coghlan, who was playing in short rightfield, and beat his off-balance throw to first baseman Yonder Alonso. Gardner never stopped running on the play and beat Alonso’s throw home with a headfirst slide.

“Great job by Joe Espada,” Girardi said of the Yankees’ third- base coach. “And for Gardy, you’re taught to run hard there because you never know.”

Yankees videos

Castro followed with an RBI single that made it 4-3 and Carlos Beltran added an RBI single in the seventh for a 5-3 lead.

“After going to Arizona and losing those first two games, sometimes you wonder, how is this road trip going to be?” said Beltran, who went 2-for-4 and finished the series 9-for-18 with eight RBIs. “But we left that in the past . . . You win some ballgames in a row and you develop a sense of, ‘OK, we’re back on track,’ and we feel like that.”

Pineda, who entered the game with a 1-5 record and 6.60 ERA, hopes this was a step in that direction for him. After allowing 11 runs and 15 hits in 10 2⁄3 innings in his previous two starts, he gave up three runs and six hits in six innings, walking one, striking out six and showing improvement with his fastball and slider.

“He made some strides today and he just needs to keep making more strides,” pitching coach Larry Rothschild said. “His fastball, he repeated some really good angles on it, hit some really good spots with it. Slider overall was pretty good. Still not as consistent as I’d like it to be, but it’s getting there.”

The A’s took a 1-0 lead in the first inning. After Aaron Hicks made a leaping catch against the wall to rob Coco Crisp of an extra-base hit, Billy Burns singled, stole second and third as Pineda paid him scant attention and scored on Stephen Vogt’s grounder to third.

advertisement | advertise on newsday

McCann led off the second with his sixth homer and Ellsbury homered with two outs in the third for a 2-1 lead. Vogt’s two-out, two-run double to leftfield in the fifth gave Oakland a 3-2 lead.

Given a 4-3 lead, Pineda pitched a 1-2-3 sixth before turning it over to the bullpen. Yankees starting pitchers have allowed seven runs, 19 hits and four walks in 31 innings during the five-game winning streak. Their ERA in that span is 2.03.

“We need to have that,” Teixeira said of the rotation’s performance of late. “You’re not going to be a championship team without a good rotation. When those guys step up, they can be as good as anybody. If they can do that consistently, I like our chances over a long season.”