MINNEAPOLIS — The Rays survived another bullpen blowup and avoided some ignominious history Sunday in beating the Twins 7-5 in 10 innings.

Yandy Diaz doubled in designated runner Danny Jansen, Jose Caballero dropped a bunt the Twins misplayed to score Diaz, and Junior Caminero followed with a sacrifice fly to provide the margin of victory.

That put a happy ending on what had the potential to be a miserable afternoon.

After the Rays scrambled to take a 4-2 lead in the top of the eighth, Mason Montgomery delivered the latest in a stretch of bullpen blowups.

Montgomery allowed a one-out walk and a home run to pinch hitter Harrison Bader.

That put the Rays in position to be walked off for a third consecutive game, something that had never happened in their 28 seasons of play.

The win was just their third in a stretch of nine games, improving them to 49-41 as they head to Detroit to face the MLB-best Tigers.

Pete Fairbanks got five outs and Eric Orze the final three.

The Rays lost 4-3 on Friday when the bullpen blew a two-run, seventh-inning lead before Bader, a former Florida Gator, homered on the first pitch of the ninth.

They then fell 6-5 Saturday when the bullpen blew a four-run, sixth-inning lead. Brooks Lee dropped a squeeze bunt to score the winning run with no outs in the ninth.

With Joe Boyle called up from Triple-A Durham on Sunday, Rays starter Drew Rasmussen likely was going to have an abbreviated outing as the team tries to limit his workload in his first season back to starting pitching following a third major elbow surgery.

Rasmussen ended up working only two innings, allowing a leadoff homer to Byron Buxton on his second pitch and not much else while throwing 32 pitches.

The Rays got even in the third in an unexpected way, as Taylor Walls led off with a 368-foot homer to right. The surprising part was that Walls was in the lineup, a late replacement at shortstop for Jose Caballero, who had right arm soreness.

(Caballero ended up playing, anyway, as he replaced Brandon Lowe after the second baseman left in the third due to left side tightness.)

The Rays went ahead in the fourth when Caballero blooped a ball to shallow right-center and hustled to second, then scored on a one-out single by Jonathan Aranda.

But Caballero then made an error that let the Twins get even in the sixth after Trevor Larnach doubled with one out. Caballero couldn’t cleanly handle a 110.3 mph smash by Brooks Lee, and when he did recover his throw to the plate was a tick late to nab Larnach.

The score remained 2-2 until the eighth, when the Rays cobbled together a rally.

Matt Thaiss and Yandy Diaz singled to get them started. Caballero dropped a well-placed sacrifice bunt, moving the runners to second and third.

Stay updated on Tampa Bay’s sports scene

Subscribe to our free Sports Today newsletter

We’ll send you news and analysis on the Bucs, Lightning, Rays and Florida’s college football teams every day.

You’re all signed up!

Want more of our free, weekly newsletters in your inbox? Let’s get started.

Explore all your options

Junior Caminero, their leading home run hitter, squibbed a ball in front of the plate, enough for pinch-runner Christopher Morel to score. Morel beat catcher Christian Vazquez, who fielded the ball, back to the plate.

The Rays then made it 4-2 when Aranda hit a slow grounder that pitcher Griffin Jax couldn’t handle.

But the struggling Rays bullpen blew another lead.

Montgomery allowed a one-out walk to Willi Castro, then gave up a two-run homer to the pinch-hitter Bader.

Monday: at Tigers

6:40, Comerica Park, Detroit

Pitchers: Rays — RH Shane Baz (8-3, 4.33); Tigers, LH Tyler Holton (4-3, 3.86) as opener

TV/radio: FS1, FanDuel Sports Sun; 95.3-FM, 620-AM, 106.7-FM (Spanish)

Info: raysbaseball.com

On deck

Tuesday: at Tigers, 6:40. Rays, RH Ryan Pepiot (6-6, 3.34); Tigers, RH Jack Flaherty (5-9, 4.84)

Wednesday: at Tigers, 1:10. Rays, RH Zack Littell (7-7, 3.50); Tigers, RH Reese Olson (4-3, 2.89)

Thursday: at Red Sox, 7:10. Rays, RH Taj Bradley (5-6, 4.79); Red Sox, TBA

Friday: at Red Sox, 7:10. Rays, RH Drew Rasmussen (7-5, 2.82); Red Sox, TBA

Saturday: at Red Sox, 4:10. Rays, RH Shane Baz (8-3, 4.33): Red Sox, TBA

Sunday: at Red Sox, 1:35. Rays, RH Ryan Pepiot (6-6, 3.34); Red Sox, TBA

July 14-17: All-Star break