Mark Teixeira and Chase Headley hit solo home runs off Koji Uehara in the ninth inning, and the New York Yankees stunned the Boston Red Sox 5-4 Thursday night to bolster their slim playoff hopes.

New York had just one hit since the third inning before Teixeira drove a pitch into the second deck in right field for his 21st home run this season. One out later, Headley homered deep into the right-field bleachers against Uehara (6-5).

Adam Warren (3-5) pitched a scoreless ninth for the Yankees, who began the night four games out of the second AL wild-card spot and are in danger of missing consecutive postseasons for the first time since 1992 and `93.

“It’s a great win, and we needed it,” manager Joe Girardi said. “And we’re going to need a lot more.”

David Ortiz homered twice and drove in three runs for the Red Sox. Brock Holt put Boston ahead 4-3 with a fifth-inning homer, then made a great sliding grab in the bottom half.

Holt’s homer broke a 3-all tie and bounced off the top of the right-field wall, just over Ichiro Suzuki’s outstretched glove, and into the seats. The second baseman made a sliding catch with his back to plate on Jacoby Ellsbury’s popup in short center leading off the bottom half, with center fielder Mookie Betts and shortstop Xander Bogaerts also closing in.

Boston starter Brandon Workman, who lost his previous eight appearances, including seven starts, gave up three runs and five hits in six innings. He remained winless since June 10 at Baltimore.

Chris Capuano, who made 28 relief appearances with Boston this year before he was released, allowed four runs and six hits in 4 1-3 innings for the Yankees. He gave up three home runs to left-handed hitters for the first time in his 239 big league pitching appearances, according to STATS.

Ortiz hit a solo drive in the first inning into the first row of the right-field seats, then made it 3-0 in the third with a two-run shot about nine rows deep. With his fifth multihomer game this year and the 46th of his career, Ortiz boosted his season totals to 32 homers and 98 RBIs — two RBIs shy of his eighth year with 100.

Derek Jeter sparked the Yankees when they tied the score in the bottom half, hitting a two-run double that bounced off the center-field wall on a hop. Jeter scored on Carlos Beltran’s two-out single through an overshifted infield.

Jeter, playing his final season, went 1 for 4 and finished with a .325 regular-season average at home against Boston, with 12 homers and 62 RBIs.

Antoan Richardson made his Yankees debut as a pinch runner in the seventh and was doubled up on Suzuki’s soft liner to center. He became New York’s 56th player this season, tying the team record set last year.

TRAINER’S ROOM

Red Sox: 2B Dustin Pedroia missed his fifth straight game because of concussion-like symptoms but likely will play Friday against Toronto.

Yankees: INF-OF Martin Prado jogged before the game, and while Girardi said Prado could return from a left hamstring strain on Friday or Saturday, the player sounded less confident. … The Yankees plan to have RHP Masahiro Tanaka (elbow) throw another bullpen session Saturday and say he must pitch in some sort of game setting to determine whether he can avoid surgery to repair a partial tear of his right ulnar collateral ligament.

UP NEXT

Red Sox RHP Allen Webster (3-3) starts Friday, with RHP Drew Hutchison (9-11) going for the Blue Jays. … RHP Michael Pineda (3-3) starts the Yankees’ series opener against Kansas City RHP James Shields (12-7).

Join our Newsletter for the latest news right to your inbox