OAKLAND, Calif. — The visitors had been held without a hit for almost six innings. A second straight loss to the A’s would have left them closer to surrendering the top spot in the AL wild-card race. All they got out of having the bases loaded and no outs was a single run. And J.A. Happ’s gem was on the way to being wasted.

Enter Luke Voit and with one swing of the bat released the pressure building inside the Yankees, who handed the A’s a 5-1 defeat in front of 17,536 at Oakland Coliseum.

“This is a good one, especially getting no-hit for a while,’’ Aaron Boone said of the win that restored the Yankees’ lead over the A’s to 4 ½ lengths. “I think we did a lot of things really well.’’

Happ gave up a one-out, bases-empty homer to Stephen Piscotty in the second and it looked like that might be the only run in the game. When Happ left after six-plus innings he had given up a run and two hits. It was a far cry from his last outing when he allowed five runs and 10 hits in 4 ¹/₃ innings to the Tigers.

“Throwing the ball with conviction, stay focused and pound the strike zone,’’ Happ said of his approach.

Right-hander Daniel Mengden followed starter Liam Hendriks to start the second and provided the A’s with 4 ²/₃ innings of shutout relief. Gleyber Torres’ two-out single to center in the sixth was the Yankees’ first hit, and he was promptly picked off first by lefty Ryan Buchter.

A bases-loaded walk issued by Jeurys Familia to Aaron Hicks scored Brett Gardner with the tying run, but the Yankees stranded three runners when, after a pair of strikeouts, Neil Walker popped up against Fernando Rodney.

Voit opened the eighth with a towering home run to left off Rodney and Hicks added an RBI single to make it 3-1. Put in the game to play defense at short, Adeiny Hechavarria homered in the ninth and Gardner later added an RBI single for a 5-1 cushion.

“It was like a playoff game. It’s fun to have emotions,’’ said Voit, who raised his right arm as soon as the ball was hit. It was his seventh homer in 19 games with the Yankees and third in as many games.

David Robertson and Zach Britton each provided a scoreless inning of relief and Dellin Betances recorded the final three outs in a non-save situation.

Happ not only recovered from a brutal outing his previous time out, the Yankees shook off a dismal 6-3 loss to the A’s on Monday in which they looked flat at the plate, where they had four hits, and in the field.

“For sure, we bounced back,’’ Happ said when asked about the win that went to Robertson (8-3). “Hopefully we get the series [Wednesday].’’

While some in the baseball world are surprised the A’s have morphed into one of the best teams in the game, Boone isn’t in that crowd.

“It’s hard to play against a great team,’’ he said. “You have to take advantage of every opportunity you get.’’

The first opportunity didn’t surface until the seventh and the Yankees could have squeezed more out of it than a run. They did better in the eighth and ninth. As a result the pressure bubbling within was released.