KANSAS CITY, Mo. (AP) The Twins had seen the scenario that faced them Thursday night play out seemingly countless times over the past few weeks: They rally down the stretch only to come up short in the ninth.

The outcome was far different this time.

Jorge Polanco drove in the go-ahead runs off ailing Kansas City closer Kelvin Herrera with two outs in the ninth, and Minnesota rallied to beat the Royals 4-2 on Thursday night in a game that could have massive implications on the American League wild-card race.

"It was really good to see us get over the hump in one of these games. We've been putting pressure on to the last out," Twins manager Paul Molitor said. "Guys put together some really nice at-bats."

Herrera, who has been dealing with a mild forearm strain, inherited a 2-1 lead before giving up a pair of singles and a walk. Brian Dozier hit a tying sacrifice fly to the warning track, and after Joe Mauer was walked intentionally, Polanco lined a single up the middle for the lead.

Trevor Hildenberger (3-2) earned the win with a scoreless eighth in relief of Twins starter Kyle Gibson, while Matt Belisle handled a perfect ninth to pick up his sixth save.

"Any time you can go seven innings without striking anybody out, your defense is playing good," Gibson said. "And hats off to the offense. That's why we won the game. The offense didn't give up."

Herrera (3-3) squandered a solid night from Sam Gaviglio, who was recently claimed off waivers from the Mariners, and the rest of the Kansas City bullpen. Gaviglio allowed only a homer from Robbie Grossman and three other hits in five innings before a trio of relievers got the game to the ninth.

"We had it lined up the way we wanted it to line up," Royals manager Ned Yost said.

The loss was especially devastating for the roller-coaster Royals given their place in the crowded AL wild-card race. They began the night 2 1/2 games back of the Twins with 24 games to play.

"That's a tough loss right there in the last inning," the Royals' Alcides Escobar said. "Just come back tomorrow, keep playing hard and win the series."

Gaviglio and Gibson matched each other through four scoreless innings before Grossman drove a 3-2 pitch from the Royals' new starter into the bullpen in right field with one out in the fifth.

Alex Gordon singled in Escobar in the bottom half, and Whit Merrifield's hard-hit single gave Kansas City a lead that would stand until the ninth.

Max Kepler started the Twins' go-ahead rally with a single. Then, after Eduardo Escobar struck out, Jason Castro singled and Grossman walked to set up Dozier, whose deep fly ball tied the game.

Polanco followed with the eventual game-winning hit.

"It was a pretty big game," Yost acknowledged. "It looked like we had everything lined up to win it but we just didn't win it."

STATS AND STREAKS

The Twins are 10-6 against the Royals this season. They were 4-15 against them last season. ... Herrera has not recorded a save since Aug. 16 at Oakland. ... Gibson has a 1.69 ERA in his last four starts. ... Mauer reached safely five times for the second time this season.

TRAINER'S ROOM

Twins 3B Miguel Sano (left shin) did light running but hit off a tee rather than taking batting practice after experiencing soreness. He has been on the DL retroactive to Aug. 20. "We'll see if he can get on the field (Friday)," Molitor said.

Royals 3B Cheslor Cuthbert was out of the lineup while dealing with a gallbladder issue. ... LHP Danny Duffy (left elbow impingement) and RHP Joakim Soria (left oblique strain) threw off the mound. "Both did OK," Yost said. "They're making progress." ... 3B Mike Moustakas was the DH to give his achy right knee a rest. "He's playable," Yost said, "or he wouldn't be in there."

UP NEXT

Royals RHP Ian Kennedy and Twins RHP Ervin Santana meet in a rematch of last Sunday, when neither factored into Kansas City's 5-4 victory. Kennedy allowed four runs, two earned, in 5 2/3 innings while Santana yielded four runs in 6 2/3 innings.