Mark Teixeira flipped his bat after a dramatic home run in Toronto over the weekend, saying “it might be my final home run.”

The soon-to-be retiring first baseman was wrong.

Teixeira hit a walk-off grand slam Wednesday night off Joe Kelly to send the Yankees to a 5-3 victory over the Red Sox in front of a wet and cold crowd of 35,520 at the Stadium.

Many of them were Boston fans who nonetheless got to celebrate an AL East title, clinched when Baltimore rallied to beat Toronto.

The Yankees (82-76) kept their very slim playoff hopes alive.

The ninth was as dramatic an inning as the Yankees have had all season.

advertisement | advertise on newsday

Trailing 3-0, Brett Gardner led off against closer Craig Kimbrel with a single, the fourth time the leftfielder reached base (two singles, two walks).

Kimbrel, erratic much of the season, walked Jacoby Ellsbury and, with Gary Sanchez at the plate, threw a wild pitch to move the runners. The rookie catcher, 0-for-3 to that point, walked to load the bases for designated hitter Brian McCann. McCann battled 10 pitches, drawing a bases-loaded walk that made it 3-1 and ended Kimbrel’s night in favor of righthander Joe Kelly, brought in to face Starlin Castro.

The second baseman struck out on three pitches. Then Didi Gregorius fell behind 0-and-2 before fouling out. Teixeira, playing in his final games before retirement, fell behind 0-and-1, then sent a laser into the Yankees’ bullpen in right-center to win it.

Yankees videos

A significant reason the Yankees are still likely to miss the playoffs for the third time in four years is their offense, which failed to perform consistently all season and which did nothing until the ninth, held to one hit until then.

Bryan Mitchell, who missed most of the season with a left big toe fracture before being reinstated from the DL Aug. 24, was terrific. He allowed two hits and walked five in seven shutout innings. The 25-year-old righthander struck out two.

Clay Buchholz, who came in 8-10 with a 5.00 ERA — but 4-0 with a 3.09 ERA in his previous six starts — allowed one hit and two walks in six shutout innings. He struck out six.

Adam Warren entered in a scoreless tie in the eighth. Sandy Leon led off with a routine grounder to second that Castro booted for an error. Dustin Pedroia, robbed twice on hard-hit balls earlier in the game, lined one to right-center for a ground-rule double to put runners at second and third.

Xander Bogaerts lined out to third and the Yankees intentionally walked David Ortiz, 0-for-8 to that point in the series, to load the bases for Mookie Betts. The rightfielder swung at a first-pitch slider and chopped one over Chase Headley’s head at third — it barely landed fair — for a two-run double that made it 2-0. After Hanley Ramirez was intentionally walked, Joe Girardi brought in lefty Tommy Layne to face Jackie Bradley Jr. A passed ball charged to Sanchez soon followed, allowing Ortiz to score to make it 3-0.