NEW YORK — If the New York Yankees reach the playoffs, they’ll long remember this catch by Brett Gardner.

Two outs in the ninth inning, bases loaded, one-run lead. Does it get much better?

Gardner made a leaping grab at the top of the left-field wall, clutching the ball as he banged into the fence on Justin Smoak’s drive for the final out, and the Yankees held off AL East-leading Toronto 7-6 Tuesday night.

“I knew I was going to be able to catch up to it and get to it, I just didn’t know if I was going to run out of room or not,” Gardner said.

“I knew it was going to be close to the wall — thank goodness it wasn’t much higher. I’m not too tall and I wasn’t going to be able to jump a whole lot higher,” he said.

A scrambly September game with October implications seesawed to the last swing, and turned at the end in favor of the Yankees. New York won its third in a row and sent the Blue Jays to their fourth loss in five games.

At a season-high seven games over .500, the Yankees closed within 4½ games of first place, their smallest deficit since April, and remained 3½ back for the AL’s second wild card.

The Blue Jays had already scored twice in the ninth off Dellin Betances when they loaded the bases with one out. Blake Parker, the eighth New York pitcher, suddenly became an unlikely closer, and he struck out Kevin Pillar.

Smoak was up next — in August 2015, he became the first Toronto player ever to hit a grand slam in the Bronx.

This time, he sliced a deep shot to left.

“Thought it might have had a chance,” Smoak said, later adding, “put a good swing on it, barreled it.”

But Gardner ran back, squared himself on the warning track and jumped high and back into the padding. He held on for the catch, immediately leaped in the air to celebrate and tossed the ball high.

“I think as the ball went in to my glove, my glove hit the wall. And on a play like that, you want to make sure the ball goes in the glove before you hit the wall,” he said.

The Yankees rushed out to congratulate him and a few minutes later, a replay of the catch drew another ovation from the fans.

The 29-year-old Smoak and 33-year-old Gardner know each other from growing up in South Carolina.

“He’s an old man,” Smoak said, smiling.

Tyler Austin’s two-run homer in the seventh off AL ERA leader Aaron Sanchez put the Yankees ahead 3-2. Pillar lined a two-out, two-run double in the eighth that gave the Blue Jays a 4-3 edge.

The Yankees then rallied for four runs in their half for a 7-4 lead, capped by Chase Headley’s two-run homer. The drive off Jason Grilli (5-5) left the reliever bent over at the mound in frustration.

With the Yankees running out of time to chase a playoff spot, manager Joe Girardi spared no expense with his staff. Chasen Shreve (2-1) wound up with the win and Parker got his first save since 2013 with the Cubs.

Betances made it interesting in the ninth, walking the first two batters while working his third straight day. Edwin Encarnacion and Melvin Upton got RBI singles on infield hits to pull Toronto within a run.

Encarnacion hit his 37th homer into the second deck for Toronto and tied his career high with 111 RBIs. He started the day tied with Angels slugger Albert Pujols for the league lead in RBIs.

Playing on his 25th birthday and batting ninth, Austin sliced a two-out drive to right-center. His only other homer came on Aug. 13 in his first major league at-bat.

Brian McCann also homered for the Yankees.

Little did anyone know what was on deck. In a game of timely hits, it was Gardner’s defensive play that thrilled the Yankees.

“Sheer joy,” Parker said.

NY NUMBERS

The Yankees had lost five straight home series to Toronto before winning the first two games in this three-game set. … Austin became the first Yankees player to homer on his birthday since Alex Rodriguez last year. … Rookie Gary Sanchez has reached base in 21 straight games.

UP NEXT

Blue Jays: RHP Marcus Stroman (9-6, 4.58) has allowed eight earned runs in 12 innings over his last two starts.

Yankees: RHP Bryan Mitchell was promoted from Triple-A before the game and is likely to make his Yankees debut this year, starting vs. Toronto. He injured a toe in spring training and missed most of this season. He’s 0-3 in stints the past two years with New York.