The walk-off win was the first for the Astros since Aug. 11 of last year, and it snapped a three-game losing streak.
"Brandon did a tremendous job," Astros manager Bo Porter said. "We were sitting there, [bench coach] Eduardo [Perez] and I, and we were saying, 'Isn't it about time we win one of these?'"
Barnes, who entered the game as a pinch-runner an inning earlier, lofted a pitch from Wilton Lopez with two outs and sent it down the right-field line. Michael Cuddyer made a long run toward the line but couldn't make the catch, setting off a celebration on the middle of the diamond.
"I knew when I got it, I hit it well enough to get it down the line and I knew it was slicing a little bit," Barnes said. "It was a matter of staying fair."
Astros starter Bud Norris escaped several jams by getting some key outs with men on base and managed to work seven innings, allowing two runs and eight hits without getting a decision. He hasn't allowed more than two earned runs in any of his seven starts at home this year (2.03 ERA).
Hector Ambriz, Jose Veras, Travis Blackley and Paul Clemens followed Norris with five scoreless innings in relief to set the stage for the Astros to win in the 12th.
"It was a good day, because the team won," Norris said. "That's the No. 1 key. The bullpen picked me up, and to have five innings after me to shut them down is huge. That offense can definitely swing the bats. Barnes had the big hit at the end, so this was definitely a team win from top to bottom."
The Rockies were 3-for-18 with runners in scoring position and stranded 15 men on base as Astros pitchers escaped numerous jams.
"A lot of times, you get into those situations and pitchers have a tendency to go faster," Porter said. "What I liked today is all of our guys kind of took a step back, took a deep breath and they made quality pitch after quality pitch. It started with Bud early in the game, and it continued throughout the rest of the game with the guys that came out of the bullpen, as well."
Colorado fell to 3-5 in extra-inning games.
"We have to find a way to win a game like that, so it's frustrating," manager Walt Weiss said. "[Barnes] just hit a ball that couldn't be placed better, right inside the line. It wasn't anything wrong with Lopez in. They hit a few on him, but he was throwing fine."
Blackley threw two scoreless innings, stranding a pair of runners in the 10th and one in the 11th.
"They managed to knuckle down and pitch well when it was all on the line," Blackley said. "We've had a fair bit of practice at it with a lot of close games, and today everyone was in sync. And I think and it's good to have one where you don't feel you've thrown a good one out there early only to have it slip away from us at the end. It was good. We needed that win."
The Astros, who left the bases loaded in the 11th, rallied against Lopez when Matt Dominguez led off the 12th with a single. Cedeno ran for him at first base and went to second on a Marwin Gonzalez bunt, putting him in position to score easily on Barnes' game-winner.
"I think these kinds of wins are definitely something that can catapult you forward from an energy standpoint," Porter said. "Fighting the way we fought as a team and to come out on top, that momentum can carry over."
The Astros didn't get a baserunner off Rockies starter Jhoulys Chacin until Jose Altuve singled with one out in the fourth inning. Jason Castro followed with a walk, and both came home on a two-out double by Carlos Pena that tied the game at 2.
Gonzalez saved at least one run in the fifth inning when he went deep into the hole at shortstop and threw out Jordan Pacheco for the final out, stranding runners at second and third. An enthusiastic Norris turned to acknowledge Gonzalez as he came off the field.
"That was a special play," Norris said. "The game was really close today, and if that squeaks through, it's 4-2 and it changes the outcome of the game completely. We played 12 innings to go out there and win the game."