NEW YORK – On a night when Mariano Rivera and Jonathan Papelbon both failed to hold ninth-inning leads, Juan Miranda drew a bases-loaded walk from Hideki Okajima in the 10th that gave the New York Yankees a dramatic 4-3 win over the Boston Red Sox on Sunday night.

New York stopped a four-game losing streak that matched its season high. The Yankees cut the magic number to one for their 15th playoff berth in 16 years and closed within a half-game of AL East-leading Tampa Bay.

Boston lost for just the fifth time in 13 games, a key defeat in an improbable September run. Boston dropped 6½ games behind the Yankees in the AL wild-card race, with just seven games remaining for the Red Sox.

Alex Rodriguez put the Yankees ahead 2-1 with his 29th homer, a two-run drive in the seventh off Daisuke Matsuzaka.

But Ryan Kalish and Bill Hall each stole two bases against Jorge Posada and Rivera as the Red Sox rallied in the ninth. Hall singled in the tying run and pinch-hitter Mike Lowell followed with a sacrifice fly.

That wasn’t the end of the late drama, with Nick Swisher, Mark Teixiera and Robinson Cano singling off Papelbon in the bottom half as a light rain fell. Cano’s hit drove in the tying run, but pinch-runner Ramiro Pena held at third. With the bases loaded, Papelbon held on by striking out Posada and retiring Lance Berkman on a flyout.

In the 10th, Curtis Granderson singled off Okajima (4-4) leading off, and Brett Gardner bunted. Catcher Victor Martinez’s throw hit Gardner for an error that allowed Granderson to take third. Martinez appeared to get hurt on the play, but stayed in the game.

Derek Jeter was intentionally walked, Boston drew the infield in and pinch-hitter Marcus Thames grounded to third baseman Adrian Beltre, who threw home for the force.

Miranda then worked the count to 3-1 before taking an inside pitch for ball four.

Boone Logan (2-0) retired David Ortiz on a squibber down the first-base line for the final out in the top of the 10th.

Papelbon wasn’t the only reliever struggling. Rivera blew a save for the fifth time in 37 chances — but third since Sept. 11. Papelbon blew a save for the eighth time in 45 chances.

With New York ahead 2-1, Jed Lowrie opened the ninth with a flyout to the right-field warning track, and Kalish singled off Rivera and stole second and third (the latter without a throw). New York brought the infield in, and with a 3-2 count Hall grounded a one-hop smash past Rodriguez’s glove at third to drive in the tying run.

Hall stole second off Posada, as the crowd booed, then stole third as Posada one-hopped a terrible throw. Lowell, appearing for the first time since taking a grounder off his jaw Friday, flied to center as Hall scored.

Matsuzaka allowed four hits, walked one and struck out seven. He had come in averaging 4.35 walks per nine innings but didn’t have any until two outs in the eighth.

Rodriguez’s opposite-field drive to right into the wind on an 0-2 pitch, was his fourth home run of the series and gave him 119 RBI.

Before the seventh, Matsuzaka allowed two runners — both on singles — and just six balls out of the infield. Mark Teixeira blooped a one-out, opposite-field single to left. Rodriguez, who had been 2-for-21 (.095) against Matsuzaka, took a strike, swung and missed, then lined a pitch into the first row of seats in right-center.