Gallardo (6-4) also hit a solo home run for the Brewers, his second in three games.
"We're probably still playing right now if he doesn't hit that homer," said Gabe Gross, who had a pair of doubles for Milwaukee. "He's kind of an all-around hero."
Gallardo began the season as the Brewers' top pitching prospect, making 13 starts for Triple-A Nashville before being called up to Milwaukee in mid-June. The seventh-inning performance Friday night showed why everyone in baseball thinks so highly of him.
Pittsburgh used three hits to pull within 3-2 with one out in the seventh and loaded the bases when the Brewers issued Jack Wilson an intentional walk with first base open.
"Wilson doesn't really walk, so trying to pitch around him is kind of a tough thing to do, and he doesn't strike out," Brewers manager Ned Yost said. "So what we were going to do is walk Wilson, load the bases and try to get the double play."
Gallardo, though, didn't let the Pirates put the ball in play. Phelps, pinch-hitting, watched a nasty curve go by for strike three, and leadoff hitter McLouth struck out swinging to end the inning with the Pirates standing helplessly on all bases.
"I just had to toughen up and bear down in that situation," Gallardo said. "I tried to get a ground ball for a double play, but I ended up striking them out. I just beared down and went after them."
The rookie rarely shows emotion on the mound, but pumped his arms as McLouth struck out to end the inning.
"I was pretty excited [about the home run], but I was a lot more excited about getting those two outs in the seventh," Gallardo said. "That was a tough situation, bases loaded and one out. I was able to get out of it and I was excited, happy."
The victory pulled the Brewers (67-67) within 1 1/2 games of the Chicago Cubs in the National League Central.
"That was big to scratch and claw out a win like that like we did," Yost said. "We've got a month left, have to keep banging away, get on a roll."
Gallardo's outing was big for Milwaukee, which has placed two starters -- Claudio Vargas and Manny Parra -- on the disabled list the past four days while getting ace Ben Sheets back after several weeks on the DL.
"You build off of this," Yost said. "Benny had a good start, Yo had a good start. [Dave Bush] had a good start [Saturday] and we're on somewhat of a roll."
Gallardo had struggled in three of his past four starts, but at times, dominated the Pirates. He pitched seven innings, giving up two runs on five hits and a walk while striking out seven.
The Brewers made it 1-0 in the first on Corey Hart's sacrifice fly and took a 2-0 lead in the third on Hart's RBI double off Tony Armas (2-5).
Gallardo did not give up a hit until Adam LaRoche hit a solo home run in the fourth inning to pull Pittsburgh within 2-1.
The rookie then hit his second home run of the season in the bottom half of the inning to make it 3-1.
"I just try to go up there and help myself out," Gallardo said. "He left a fastball over the plate, I tried to put it in play and it worked out good. I was able to get it up in the air and I think he made a mistake."
Francisco Cordero pitched the ninth for his 38th save in 44 chances.
Rickie Weeks went 0-for-3 with a walk to end his 10-game hitting streak.
After the game, the Brewers said they would make several moves as part of the September callups. They will recall utility man Vinny Rottino and right-hander Carlos Villanueva from Nashville, and purchase the contracts of right-hander Chris Spurling and left-hander Mitch Stetter from Nashville. They also will purchase the contract of outfielder Mel Stocker from Double-A Huntsville, transfer third baseman Corey Koskie to the 60-day disabled list and add Charlie Green to the coaching staff.