Catcher Jonathan Lucroy also delivered a two-out, two-run hit for the Brewers, who were handed an early lead by a wild Tim Lincecum but had to wrestle it back later on. Reliever Kameron Loe picked up the win, and Jose Veras, Francisco Rodriguez and John Axford closed the door on the Giants.
Ramirez's go-ahead hit came against one of the Giants' toughest relievers, Sergio Romo.
"It feels good, you know, because we ended up winning the game because of that," Ramirez said. "That's part of my job, to get game-winning hits and come through in big situations."
Ramirez signed a three-year deal with the Brewers in December and has a lot of work ahead to reach his usual level of production. After going 1-for-3 with a walk and a run scored, he's batting .211 and sits fourth on the team with 12 RBIs.
"He was getting a little frustrated with things," manager Ron Roenicke said. "So that was a big hit that he got there."
The starting pitchers finished the night as footnotes -- Lincecum was wild early and sharp late in a five-inning outing; the Brewers' Zack Greinke was sharp early and then hittable late, particularly in a three-run Giants' sixth inning that finished with the teams tied at 4.
The Brewers reclaimed their lost lead quickly, rallying against losing pitcher Clay Hensley and Romo. Norichika Aoki and Rickie Weeks reached on successive infield singles off Hensley, and two batters later, with Romo in the game, Ramirez delivered the decisive hit.
The Brewers got to Lincecum for three runs in the first inning, a breakthrough considering Milwaukee had been shut out in its two previous games. The Brewers had been blanked in three straight games only once in franchise history -- from May 3-6, 1972, when a couple of Hall of Famers (Bert Blyleven and Nolan Ryan) won the first two games and Andy Messersmith made it three in a row.
Lincecum himself ensured the 2012 Brewers would not repeat that bout of offensive ineptitude. He hit Carlos Gomez with the game's second pitch, walked Weeks, induced a Corey Hart fielder's choice grounder but then threw a wild pitch for a 1-0 Brewers lead.
Each of Lincecum's first seven pitches and 12 of his first 14 pitches missed the strike zone, many of them fastballs that ran way inside to right-handed hitters. Lucroy capitalized on a rare strike and delivered a two-out, two-run single that made it 3-0.
"I was going too quick to home plate," Lincecum said. "I wasn't really giving my arm a chance to catch up, so balls were flying into the right-handed batter's box."
The question was whether the Brewers had done enough.
"I think we should have scored more because he was wild," Ramirez said. "It wasn't his best night. But that's what good pitchers do, he battled through it and ended up pitching pretty good."
Roenicke had the same worry.
"That's the problem -- you want to score as many runs as you can when you get him in trouble there early," Roenicke said. "And then he shuts us down for a while. I was worried we weren't going to get anything else off him."
Indeed, Lincecum did not allow another run over the rest of his five-inning start. Greinke matched him, blanking the Giants until a fifth inning that began with three singles but produced only one run. After former Giant Travis Ishikawa extended the Brewers' lead to 4-1 with a suicide-squeeze bunt in the top of the sixth, the Giants made their move.
They sent eight men to the plate against Greinke and Loe and scored three runs for a 4-4 tie, the crucial run coming on an error charged to Weeks. He was stationed at the edge of the infield grass after Lucroy's passed ball put the tying runner at third base, and couldn't come up with a Brandon Belt ground ball to Weeks' left.
Greinke was at something of a loss when asked about the inning that got away.
"That stuff happens to me too much," he said. "I just have to make better pitches, because most pitchers can get out of that stuff, and I didn't. [Loe] came in and made some good pitches and the [rest of] the bullpen did the same thing."
Roenicke didn't have any answers for Greinke's sudden struggle.
"He was cruising," Roenicke said. "I thought he pitched a great game up until that point. And then, I don't know if he got tired? I don't know. All of a sudden, pitches were elevated in the zone. He stopped hitting that low and away fastball he had been so good with all game. To that point, he pitched a great game. I don't know what happened there."
Loe was the winning pitcher after the Brewers scored in the seventh. Axford's save was his sixth this season and 49th consecutive conversion dating to last season.
"Their bullpen did a pretty good job on us," Giants manager Bruce Bochy said. "But I liked the way [the Giants] were fighting tonight."