The Pirates were down 3-2 in the sixth inning when Garrett Jones reached on first baseman Matt Downs' throwing error, went to third on a double play and scored the tying run on a passed ball by catcher Carlos Corporan.
Pittsburgh later grabbed the lead in the eighth.
Andrew McCutchen walked and Jones singled off Chuckie Fick (0-1) to start the frame and two outs later Barajas lined a solid hit to left field off Fernando Rodriguez, handing the Pirates their 30th come-from-behind win of the year.
The rally made a winner of Chris Resop (1-3). Jason Grilli picked up his second save as Pittsburgh won its fourth straight.
The second-place Bucs came into the contest trailing National League Central-leading Cincinnati by two games.
Justin Maxwell drove in two runs for free-falling Houston, which lost for the 25th time in 27 games overall.