Yankees' Starlin Castro , right, and Todd Frazier high-five after...

Yankees' Starlin Castro , right, and  Todd Frazier  high-five  after Castro  homered  on Sept. 26, 2017, at Yankee Stadium. Credit: Joseph D. Sullivan

Tuesday night the Yankees clinched what has seemed inevitable for weeks: home-field advantage for next Tuesday’s American League wild-card game. But with their 6-1 victory over the Rays at the Stadium, a far more desired goal, though still a long shot, remained in play: the East crown.

Coupled with the Red Sox’s second consecutive loss to the Blue Jays, the surging Yankees crept within three games of Boston with five left.

“I know time’s not on our side, but you never know,” Chase Headley said. “Play it out. Try to put pressure on (the Red Sox) to perform. But it’s nice to know worst-case scenario we’ll play that (wild-card) game at home.”

The way things are going for the Yankees (88-69), winners of 17 of their last 23 games, winning out suddenly isn’t beyond the realm of possibility. After two more against the Rays (76-81), the Blue Jays (75-83) come to town for three.

“There’s still a chance,” Joe Girardi said. “We have to play really well, and that still is our focus, that we play extremely well. And then you see what happens.”

Regardless, the Yankees have the look of a club that will be difficult to beat in October, and they added to those reasons Tuesday night.

Aaron Hicks, a first-half standout before suffering a series of injuries, returned from the latest one (left oblique strain) and made an immediate impact. The centerfielder robbed Wilson Ramos of a grand slam in the first inning, bringing his blast back over the wall after making a leaping catch at the 385-foot sign in right-center.

“It saved the day,” Girardi said.

It seemed to settle down lefthander Jordan Montgomery, who allowed the run that came in on that play, a sacrifice fly, but nothing else over six innings. Montgomery (9-7, 3.96) is slated to start Sunday. He likely will be out of the rotation should the Yankees make it to the Division Series, but he’s trying to make that decision as tough as he can. He allowed six hits and one walk, and struck out five. After Montgomery completed his final inning, he embraced Hicks in the dugout as both smiled.

“I told him, ‘Thanks for saving me,’ ” Montgomery said.

Tommy Kahnle and David Robertson pitched scoreless innings and, thanks to Gary Sanchez’s and Matt Holliday’s back-to-back RBI singles in the eighth that made it 6-1, the ninth was a non-save situation. Dellin Betances, whom the Yankees are trying to get on track for the postseason, pitched a perfect inning.

The Yankees did most of their damage in the four-run second, driving lefthander Blake Snell (4-7) from the game. They sent eight to the plate, with Starlin Castro, who had three hits, leading it off with his 15th homer to tie it at 1, and Hicks and Aaron Judge drawing back-to-back bases-loaded walks.

But the most exciting sequence of the night came in the first as the Rays (76-81) took the lead but left the inning feeling somewhat empty. Kevin Kiermaier and Steven Souza Jr. led off with singles and Evan Longoria walked to load the bases. Montgomery struck out Logan Morrison, setting up Hicks’ moment. Ramos jumped on a first-pitch changeup and sent it to center. Hicks, on the dead run, leaped at the track and timed his jump perfectly, the ball landing in his glove, which was over the wall, for an out.

It was the second grand slam Hicks denied this season, matching a June 14 effort in Anaheim when he robbed the Angels’ Luis Valbuena.

“Changed the complexion of the game,” Headley said. “You’re not going to see a better play than that. I’m not going to say it’s the best play I’ve ever seen, but I haven’t seen many that are better than that.”

More Yankees headlines