The Yankees have the best record in the majors. The A’s have the worst record in the majors.

And that was the backdrop when their three-game series at Yankee Stadium began Monday night.

The Yankees had to work hard to come back twice in a split of the previous four-game series with the Astros, a possible look ahead to the postseason. Oakland, of course, figures to be off during the postseason. The A’s also brought the majors’ second-lowest payroll and lowest collective batting average by a lot — .211.

They didn’t look the part of Bronx doormats early on, though. The Yankees had work to do after falling into a four-run hole when Oakland scored five in the third.

But this team is good at climbing. The Yankees went ahead for good with six runs in the seventh — helped by two-run doubles by Josh Donaldson and Jose Trevino along with two catcher’s-interference calls — and won, 9-5, for their major league-leading 23rd comeback victory.

“We feel good obviously about our chances to come back and win,” Donaldson said. “ . . . At the end of the day, we believe in our lineup and we believe in our ability to score runs. So we really feel we’re not out of a game.”

They’re up to 54-20, the third-best start in franchise history through 74 games.

Now for the latest comeback:

It was 5-1 Oakland when it began. Paul Blackburn, who gave up Anthony Rizzo’s 20th homer in the first, served up Giancarlo Stanton’s 18th in the fourth. That gave him five hits in his last 10 games, all homers.

The Yankees cut it to 5-3 in the fifth. DJ LeMahieu drew a two-out walk and stole second. Aaron Judge, the major-league leader with 2,433,088 votes in the latest All-Star balloting results, ripped an RBI single off the glove of a leaping Elvis Andrus at shortstop.

The A’s, who are 10-30 in their last 40 games and 25-50 overall, began to self-destruct in the seventh.

The Yankees loaded the bases without a hit as Adam Oller walked LeMahieu, Judge reached on catcher’s interference by Sean Murphy and A.J. Puk drilled Rizzo in the right elbow to load the bases.

Then Murphy interfered with Stanton’s swing, so the catcher’s second error of the inning made it 5-4.

“We’ve done it in a lot of different ways this year,” Donaldson said. “Obviously, catcher’s interference is unique.”

Donaldson was next, and he lined an  0-and-2 fastball to leftfield for a two-run double and a 6-5 lead.

Austin Pruitt came on, and with two outs, Trevino belted a two-run double that hit the leftfield wall on a hop — 8-5. Marwin Gonzalez singled home Trevino to make it a six pack of runs for the Yankees.

“It’s a dangerous offense,” manager Aaron Boone said.

Albert Abreu earned the win in relief of Jordan Montgomery.

The lefty had his season-opening run of 13 straight starts allowing three runs or fewer snapped in his previous outing, when he yielded four across six innings in a no-decision at Tampa Bay. Then he extended his streak of allowing more than three runs to two because of one bad inning.

After allowing a walk and picking up two strikeouts to begin the third, Montgomery yielded a single by Christian Bethancourt and an RBI double by Ramon Laureano off the very top of the leftfield wall to tie it at 1. Montgomery hit Murphy with a one-bounce 0-and-2 pitch to load the bases and watched Andrus drive in three runs with a double to left. Sheldon Neuse then singled to make it 5-1.

Montgomery hung in there, though, and wound up going 6 2⁄3 innings.

“I’m still sick about that third inning,” he said, “but I kept us in it.”

More Yankees headlines