New York’s Brett Gardner, right, douses the Yankees’ Mark Teixeira (25) with purple Gatorade after the Yankees defeated the Boston Red Sox 5-3 on Teixeira’s ninth-inning, walk-off grand slam. The Red Sox still clinched the American League East title. less

NEW YORK >> Mark Teixeira hit a game-winning grand slam with two outs in the ninth inning, and the New York Yankees staved off playoff elimination at the last possible moment with a 5-3 victory over the Boston Red Sox on Wednesday night.

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.

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) on his way in from the bullpen.

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, planning to retire after this season, launched a 99 mph fastball on an 0-1 count into the New York bullpen in right-center.

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.

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 bubbly.

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.

Teixeira hit the Yankees’ first game-ending slam since Alex Rodriguez on April 7, 2007.

James Pazos (1-0) was the winner, 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 for all to see, 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.

Mookie 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 postseason schedule Oct. 6 seeking a second World Series crown since 2013. The team’s opponent has not been determined yet.

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, manager Joe Girardi said.

UP NEXT

Ortiz, set to retire 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.