MINNEAPOLIS -- The Tampa Bay Rays were wrapping up a rough road trip, on the verge of another frustrating defeat as they stranded runner after runner on base.

One of the best bullpens in baseball made sure they at least left Minnesota on a winning note.

Yandy Diaz hit a sacrifice fly in the 18th inning to break a tie that had lasted since the second, and the Rays finally pulled away for a 5-2 victory on Thursday to avoid being swept in a series by the Twins for the first time in 13 years.

"That was kind of the motto today at some point. We were like, 'If we're going to play this long of a game, we're going to win the game,'" said Ryan Yarbrough, the eighth reliever and 22nd player used by the Rays during the game that lasted 5 hours, 42 minutes after rain delayed the first pitch by 57 minutes.

Yarbrough (7-3) threw three of the 16 scoreless innings logged by Rays relievers to earn the victory. The bullpen that entered the game with a 3.51 ERA, good for third lowest in the majors, allowed six hits against 19 strikeouts.

"We're a really tight group, not just teammates, but friends. We root for each other so hard," said Adam Kolarek, who pitched the 13th, 14th and 15th innings. "So I think as each inning kind of passed and each pitcher went in, it was like you want to keep the good outcomes coming."

The Twins had the same vibe going, with their first eight relievers combining to allow two hits over 10 innings. Starter Kyle Gibson joined the club in the 17th inning, his first relief appearance in 170 career games. Then Ryne Harper (3-1) faltered in the 18th, pitching for the third straight game and the sixth time in 10 days.

The Rays loaded the bases with nobody out, just as they did in the 10th against Blake Parker when they failed to score. Diaz hit a medium-depth fly ball with one out to left fielder Luis Arraez, whose strong throw was a little too far up the first-base line for catcher Jason Castro to catch and make the tag before Brandon Lowe's left hand grazed the plate on his headfirst slide.

Rays pitchers set a franchise record with 22 strikeouts. This matched their longest game in history in terms of innings. This was also the most innings played in Target Field's 10-season history. The Twins beat the Boston Red Sox in 17 innings just last week.

The Twins, who were missing three regulars from their lineup to injuries, went without a home run in two straight games for the first time since May 30-31, also against the Rays.

"They're character-building games for sure, and with any team you look for your identity in the way that you're going to play, and our guys, they never quit," Twins manager Rocco Baldelli said. "We've been kind of spoiled with the way that we've swung the bats this year, and when we have a little run where we don't swing the bats like that, they're easy to point to. These games are tiring."

Willy Adames and Ji-Man Choi followed Diaz with RBI singles to give the Rays some insurance on their way to salvaging a 3-7 record from this three-time zone road trip. Just 13 days ago, they led the American League East by a half-game over the New York Yankees, who are tied with the Twins for the AL's best record. Now the Rays are 6½ games back in the division race.

"We got to the point where we'll take any win however we get them," manager Kevin Cash said. "That was a challenge."

WAY BACK WHEN

Ryne Stanek made his major league-leading 23rd start for the Rays in their trendsetting opener role, falling behind 2-0 in the first of his two innings. Arraez had the third hit of the frame for the Twins, a sharp two-out single to right field that prompted third base coach Tony Diaz to wave Nelson Cruz home.

The throw by Guillermo Heredia would've beaten the 38-year-old designated hitter by a long way, but the ball skipped past catcher Mike Zunino for an error on Heredia as Cruz safely reached the plate.

STRONG START

The Twins, who lead the major leagues in runs and several other hitting categories, left starter Martin Perez hanging after he allowed only three hits over seven stellar innings. After allowing consecutive two-out RBI in the second that tied the game -- a double by Michael Brosseau in his first major league start and a single by Heredia -- Perez finished his afternoon by retiring 16 consecutive batters. Then he watched the next, yes, 11 innings.

"It was fun," said Perez, who was 0-2 with a 7.50 ERA in his previous five turns. "I know we're a little tired, but that's baseball."

TRAINER'S ROOM

Rays: Diaz was reinstated from the injured list after the minimum 10-day stay and played 3B. ... CF Kevin Kiermaier departed with tightness in his lower right leg in the second inning. Cash said later he was being overly cautious about pulling the speedster from the game.

Twins: The multi-positional Willians Astudillo was placed on the 10-day injured list with a strained left oblique. ... LF Eddie Rosario was held out with a sprained left ankle suffered the night before.

UP NEXT

Rays: RHP Yonny Chirinos will start the series opener on Friday night against Texas. RHP Lance Lynn (9-4, 4.32 ERA) takes the mound for the Rangers, as the Rays start a 10-game homestand that lasts until the All-Star break.

Twins: RHP Jose Berrios (8-3, 2.84 ERA) will take his 17th turn of the season in Chicago on Friday night, opening a three-game series. Berrios has a 1.63 ERA in four starts this month. The White Sox were waiting to announce their starting pitcher.