MILWAUKEE -- The third go-ahead hit was the charm for the surging Brewers.

Gabe Kapler gave the Brewers a 2-1 lead with a seventh-inning single and Mike Cameron made it 3-2 in the eighth when he homered as part of a career-best five-hit afternoon, but both times the Pirates answered. They didn't get a chance after Brewers shortstop J.J. Hardy -- on his own bobblehead day -- singled home Rickie Weeks in the bottom of the 12th inning for a 4-3 Brewers win at Miller Park on Sunday.

"[Jason] Kendall said before the game that I was guaranteed two or three hits or a homer, because it was my bobblehead day," a smiling Hardy said after going 3-for-5. "I didn't know about that."

CC Sabathia pitched six innings in his 10th Brewers start and settled for a no-decision while the Brewers finished a three-game sweep and a 5-1 homestand. They remained 4 1/2 games behind the division-leading Cubs in the National League Central and 3 1/2 games ahead of the Cardinals for the NL Wild Card.

Hardy notched the winning hit off right-hander Jason Davis (1-4), but if there was a ceremonial game ball, it belonged to Brewers reliever Guillermo Mota (4-5). The right-hander earned a standing ovation for his Houdini act in the top of the 12th from a Miller Park crowd that has been tough on him this season.

Mota inherited a bases-loaded, no-out jam from Carlos Villanueva, who pitched a scoreless 11th inning but allowed a single and a pair of walks leading off the 12th. The fact that Pirates catcher Ryan Doumit was the runner at third was a break for Mota, because Doumit didn't break for home when Mota retired the first hitter he faced on a flyout to medium center field.

Mota, who was one of two remaining options for Brewers manager Ned Yost -- Eric Gagne had already warned up in the eighth and Yost was unwilling to get him hot again -- then struck out Chris Gomez and fielded Luis Rivas' grounder back to the mound to preserve the 3-3 tie.

"That definitely changes the swing of the momentum," Cameron said. "He got a flyout and a big strikeout and then a little tapper back to the mound, and those things definitely played a role into how you're going to mount your attack in your next two at-bats."

Said Hardy, "It was a good time to win the game after that inning." Davis made a nice play to throw out Craig Counsell on a soft grounder to open the bottom of the 10th inning, but Weeks worked a one-out walk and then stole second base. Hardy won the game when he lined an opposite-field single over the second baseman.

Cameron finished 5-for-5 with a pair of RBIs, including an eighth-inning solo home run that gave the Brewers a 3-2 lead entering the ninth. The Pirates rallied against Salomon Torres, handing the Brewers closer his sixth blown save when pinch-hitter Nate McLouth singled in a run with nobody out. McLouth had missed the previous four games with a viral infection.

Pittsburgh mounted a similar comeback in the seventh inning against David Riske, who was trying to preserve Sabathia's win following Kapler's clutch hit. But pinch-hitter Freddy Sanchez tripled with one out past a diving Cameron in center field, and he scored when Nyjer Morgan golfed a low pitch for a bloop single.

Sabathia worked six innings in the shortest of his 10 starts as a Brewer. Coming off a complete-game, 130-pitch win over the Astros earlier in the homestand, he scattered eight hits but did not walk a batter and allowed only one run.

His command was not there on Sunday, Sabathia said. Which begged the question: Did his much-discussed, 130-pitch outing have anything to do with that?

"I don't think so," said Sabathia, who was pushed that far in part because he had an extra day of rest before facing Pittsburgh. "I think maybe having six days off had more to do with it than anything. But I felt I made some pitches when I needed to, and I kept us in the game."

Sabathia allowed his only run when Morgan, Jack Wilson and Doumit hit consecutive singles with two outs in the fifth inning. Wilson and Doumit both hit two-strike pitches, and Doumit's bloop single fell in front of Cameron.

"That's them having good at-bats," Sabathia said. "They got three base hits with two outs, and I had two strikes on a couple of those guys. They did a good job of hanging in there and making their inning stretch."

Pirates ace Paul Maholm also had to work in and out of trouble during his six-inning stint. The Brewers loaded the bases with no outs in the third inning and the heart of their order coming up, but Ryan Braun struck out and Prince Fielder hit into a double play. They loaded the bases again in the fourth with one out, but Sabathia and Weeks struck out.

"He's very good," Hardy said of Maholm, a left-hander who had a 2.80 ERA in his previous five road starts. "He has good stuff, and it just makes it tougher when you have those shadows out there."

The Brewers sold out their 21st consecutive game on Sunday, and the crowd of 45,163 marked the fourth largest this season and the sixth largest in the team's eight seasons at Miller Park.