NEW YORK — His playing days dwindling, Mark Teixeira gave himself a moment to savour Wednesday night and rescued the New York Yankees in the process.

Teixeira hit a game-winning grand slam with two outs in the ninth inning, and the Yankees staved off playoff elimination at the last possible instant with a 5-3 victory over the Boston Red Sox.

"That's as good as it gets right there," said Teixeira, set to retire after this season.

Boston clinched the AL East title nearly 30 minutes before Teixeira connected, thanks to Toronto's 3-2 loss against Baltimore. But a five-run comeback by New York in the ninth prevented its longtime rival from celebrating on the Yankee Stadium field.

"It's an unbelievable moment," manager Joe Girardi said. "It's a great moment for him, and it's a great moment for us."

The victory by the Orioles put the Yankees on the brink of wild-card elimination following a surprising charge late this season. New York had only one infield hit and trailed 3-0 heading into the ninth, with Red Sox closer Craig Kimbrel (2-5) jogging in from the bullpen on a misty and blustery night in the Bronx.

Kimbrel, however, gave up a leadoff single to Brett Gardner and issued three straight walks, the last forcing in a run.

Red Sox manager John Farrell turned to Joe Kelly, who struck out Starlin Castro and retired Didi Gregorius on a foul popup. With the Yankees down to their last out, Teixeira launched a 99 mph fastball on an 0-1 count into the New York bullpen in right-centre.

"You don't want a wall-scraper in an 8-0 game to be your last one. You want a walkoff grand slam against the Red Sox," Teixeira said. "I'll still be trying the next four games, but if it happens to be my last one, it will be pretty special."

Yankees relievers raised their arms in excitement, and Teixeira puffed his cheeks and spread his arms wide as he rounded first base. The 36-year-old slugger, in his 14th big league season, tossed his helmet as happy New York players poured out of the dugout to swarm him at home plate.

"I'm going to take away a lot of memories. This will be one of the best," Teixeira said. "That's as good as I can hit a ball."

New York (82-76) guaranteed its 24th consecutive winning season, the second-longest streak in major league history behind the Yankees' run of 39 straight from 1926-64.

It was the team's first game-ending slam since Alex Rodriguez on April 7, 2007.

Meanwhile, the Red Sox walked back to their bench and quietly cleared out of the dugout, an awkward way to mark their second division crown in four years. But once they returned to their clubhouse, they let loose and celebrated with the traditional beer and bubbly.

"Can't let one inning deter our whole season," outfielder Mookie Betts said.

James Pazos (1-0) earned his first major league win, and the Yankees remained four games behind Baltimore for the second AL wild card with four to play.

Though the final score in Toronto was posted on the out-of-town scoreboard, the Red Sox hardly seemed to notice as players draped their arms over the dugout railing and rooted for Kimbrel in the ninth.

Red Sox executives, however, jumped out of their front-row seats next to the Boston dugout and rejoiced when the Blue Jays' game ended, giving Boston its eighth AL East title.

Betts snapped a scoreless tie when he chopped a two-run double over third base in the eighth inning. David Ortiz scored moments later on Gary Sanchez's passed ball.

Boston, which had already secured at least a wild-card spot in the playoffs, will open its post-season schedule Oct. 6 seeking a second World Series crown since 2013. The team's first opponent has not been determined yet.

"You never want to see a team celebrate on your home field, or celebrate at all, so we'll take that as a positive tonight," Teixeira said in the clubhouse, where he was congratulated by Hall of Famer Reggie Jackson.

TRAINER'S ROOM

Red Sox: LHP Drew Pomeranz (sore forearm) is tentatively slated to throw his customary 30-35 pitches in a bullpen session Thursday. The team hopes he can make a relief appearance during its final regular-season series this weekend, but manager John Farrell said that won't be determined or planned until Pomeranz gets through his bullpen OK. ...

Yankees: RHP Masahiro Tanaka (forearm) felt fine after playing catch Tuesday, but the team is unsure if he'll start Saturday against Baltimore, Girardi said.

UP NEXT

Ortiz, also retiring after the season, says goodbye to Yankee Stadium in the series finale Thursday night. New York has planned a pregame ceremony.

Red Sox LHP Henry Owens (0-1) starts in place of Pomeranz, scratched from his scheduled outing. Owens threw 80 pitches at the team's complex in Florida last week and should be comfortable going that far, Farrell said. LHP CC Sabathia (8-12) gets the ball for the Yankees.