The Pirates fell one game behind NL Central-leading St. Louis. The Cardinals were idle, and start a three-game series at Pittsburgh on Friday night.
Making his Pirates' debut, catcher John Buck went 3 for 3 as he subbed behind the plate while starter Russell Martin got the night off. Pittsburgh acquired Buck and outfielder Marlon Byrd from the New York Mets on Tuesday.
Gallardo (10-9) allowed six hits and one walk while striking out five. He improved his career record against the Pirates to 11-4, including 5-2 at PNC Park.
Gallardo has given up just two earned runs in 19 1-3 innings in three starts since coming off the disabled list. He was sidelined with a strained hamstring.
Brandon Kintzler pitched the final two innings of the six-hit shutout.
Ramirez hit his ninth home run of an injury-marred season to lead off the fourth inning and put Milwaukee ahead 3-0. Ramirez, who broke into the major leagues with Pittsburgh in 1998, hit his 350th career homer Tuesday in the series opener.
Rookie Gerrit Cole (6-7) pitched a career-high 7 1-3 innings in his 14th start. He allowed four runs and 10 hits.
The 22-year-old was pitching on seven days of rest as the Pirates continue to find spots to limit his workload. Cole has pitched a combined 153 1-3 innings between Pittsburgh and Triple-A Indianapolis after working 132 last year in his first professional season.
A throwing error by Cole led to the Brewers scoring a first-inning run. Cole threw wildly to first on a pickoff attempt and Jean Segura advanced two bases before scoring on Jonathan Lucroy's single.
The Brewers added another run in the second when Carlos Gomez led off with a single, stole second and scored on rookie Khris Davis' double.
Lucroy drove in his second run with a sacrifice fly in the eighth to make it 4-0.
Rookie second baseman Scooter Gennett had three hits for Milwaukee and Segura and Gomez added two each.
Pittsburgh put runners in scoring position in three of the first four innings but came empty. Gallardo then set down nine of his last 10 batters before being lifted for a pinch-hitter.
NOTES: Pittsburgh LF Starling Marte, who has been on the disabled list since Aug. 19 with a bruised right hand, has been swinging a bat with his left hand and may be able to start taking batting practice next week, a week earlier than planned. ... Milwaukee RF Norichika Aoki didn't start. He has been getting regular days off to rest his sore right knee. He grounded out for Gallardo. ... Pittsburgh LHP Francisco Liriano (14-6, 2.74) is scheduled to start Friday night against St. Louis RHP Shelby Miller (12-8, 2.90). ... The Brewers face the Los Angeles Angels on Friday in the opener of a three-game series with RHP Wily Peralta (8-13, 4.51) facing RHP Jered Weaver (8-7, 3.46).