"You wish you could have just closed it out there [in the ninth], but we didn't," Porter said. "They did a good job of coming back and getting the win."
The winning run scored on a wild pitch by Astros reliever Kevin Chapman, who had his streak of 13 consecutive appearances to begin his career without allowing an earned run come to an end. The lefty allowed three runs and three hits in the 12th for his first career loss.
It was only an hour earlier when the Astros were down to their last strike with nobody on base against Glen Perkins, who hit Brett Wallace with a two-strike pitch and allowed a single to L.J. Hoes before Barnes shot an 0-2 pitch over the right-field wall to tie the game.
"We haven't lost the lead too many times with Perk in the game," Twins manager Ron Gardenhire said. "He just didn't have his stuff, though. Kid hit a fastball out and over the plate, about 95 mph, in the seats, and there you have it. He just couldn't locate the baseball, couldn't get it where he wanted to and they made him pay for it."
Barnes, who's hitting .375 in his last 12 games, went 3-for-4 with a career-high five RBIs.
"We've been fighting all year," he said. "We've been on the wrong side of some games, but we fight and we battle and we're young, and we're still learning."
Cosart, who entered the game with a 1.59 ERA in his first eight big league starts, labored through four innings and surrendered a career high four earned runs. He threw 90 pitches and failed to pitch at least five innings for the first time in his career.
"It's kind of what everybody has been waiting for," Cosart said. "I just didn't get the results. A starter's job is to keep the team in the game, and I didn't do that today. I thought I made one really bad pitch, and I fell behind some hitters and that's kind of how it went."
Cosart said he struggled to throw his breaking pitches over the plate consistently, but Porter thought the problem was fastball command.
"Whenever your pitch count's spiked that high, that fast, it's just a lot of deep counts and he just did not have a lot of fastball command," he said. "He wasn't able to get to his put away breaking stuff because he wasn't commanding his fastball."
Brian Dozier hit a two-run homer off Cosart in the first inning, and Trevor Plouffe hit a two-run homer in the third that followed a two-out walk and put the Twins ahead, 4-1. Chris Carter led off the second inning with a homer for the Astros.
Carter is the first Astros player to hit at least 27 homers in a season since 2008, when Lance Berkman hit 29 and Carlos Lee hit 28.
Trailing 4-1, the Astros cut into Minnesota's lead in the seventh inning on a two-run, two-out single by Barnes off reliever Casey Fien. The inning continued with the Astros putting runners on the corners with two outs, but Barnes was thrown out at the plate trying to score after Marwin Gonzalez was caught in rundown between first and second and Twins first baseman Chris Colabello briefly dropped the ball.
"He actually just broke too early," Porter said of Gonzalez. "I thought he had a chance once they dropped the ball, but they were able to recover the ball and throw Barnes out."
The Twins took a 6-3 lead in the eighth, when Alex Presley doubled home Josmil Pinto and then scored on a sacrifice fly one pitch after advancing to third on a wild pitch.
The Astros got three scoreless innings from both Lucas Harrell and Josh Zeid, who worked the ninth, 10th and 11th before handing the ball to Chapman.
"Lucas came in and did a good job, and Zeid came in and we stretched him about as far as we could stretch him," Porter said. "After that, they were able to get a few runs there off the next two guys, but we came back and tied it and they get the last few."