Cain (13-4) scattered four hits and pitched into the eighth inning, matching his career high for wins after going seven starts without one.
"He's bull strong," Giants manager Bruce Bochy said. "He's had some tough luck, but he's more of a pitcher now and he's keeping his composure out there."
Juan Uribe homered to spark a two-run rally in the fourth inning and San Francisco won for the sixth time in eight games. The Giants began the day one game behind Colorado for the NL wild card. The Rockies hosted Arizona later.
"You can see the end of the regular season but we're shooting for, obviously, the playoffs," Cain said. "There's a lot right now riding on everything. So you go out there and every start you know stuff's on the line."
After Jeremy Affeldt got an out, Wilson got the final five for his 34th save, reaching as high as 102 mph on the stadium radar gun.
"It might have been a little juiced," Cain said. "But I'll give it to him."
Wilson, who pitched in a save situation the night before, said he felt as though he was throwing a little harder than usual.
"This is a race. There's no time to just sit and wait," Wilson said. "If they need me to come in the eighth, seventh, whatever, now go out there and get the outs and then pass the buck on to the next guy."
On Sunday, San Francisco will try to sweep the Brewers in Miller Park for the first time in nearly five years.
This one got interesting late, when Cain came out to start the eighth after getting through a rocky seventh.
Leading 3-1, the right-hander allowed a leadoff double to Jason Kendall and was pulled for Affeldt. Kendall moved to third on pinch-hitter Mike Cameron's groundout and scored on Felipe Lopez's infield single to cut the lead to one.
Affeldt then threw the ball into center field for an error on pinch-hitter Jason Bourgeois' tapper back to the mound, putting runners on first and second.
But Wilson got Ryan Braun to hit into a fielder's choice and Prince Fielder, who drove in his major league-leading 122nd run in the fourth, hit a bouncer that second baseman Eugenio Velez flipped to Uribe at second to end the threat.
Wilson worked around a one-out single by Jody Gerut in the ninth. Gold Glove center fielder Aaron Rowand and right fielder Randy Winn collided on the final out, a fly ball caught by Rowand after Winn slowed down at the last moment.
"It really shows his maturity as a closer," Cain said about Wilson. "He's not just one guy that can go out there and get three outs and that's it. He's a guy that can go out there and pitch an inning, two innings or whatever."
Cain, who hadn't won since July 24, was sharp through the first six innings, retiring 10 of his first 11 batters and striking out the side in the third. He finished with five strikeouts and worked around consecutive walks to start the seventh.
Uribe homered off Gallardo (12-11) with two outs in the fourth. Nate Schierholtz followed with a single and scored when Rowand doubled down the left-field line. Winn's run-scoring double in the fifth made it 3-1.
"I left one change up and over the plate and I paid for it. Everything was going very good today until that fourth inning," said Gallardo, who pitched six solid innings and struck out six.
Bochy said second baseman Freddy Sanchez (shoulder) would rejoin the team Monday, but he'll find a place for Uribe, who is hitting .321 over his last 17 games with five homers and seven RBIs.
"We knew in spring training that we were getting a good player, but until you watch him on a daily basis, you get to appreciate some things he can do and how versatile he is," Bochy said. "When he gets hot he can carry a ballclub."
Gallardo is eight strikeouts from becoming the fourth Brewers pitcher with 200 in a season (Ben Sheets, Teddy Higuera and Doug Davis). ... Milwaukee Mayor Tom Barrett, attacked by a man with a metal bar last month, threw out the first pitch with his left hand. The 55-year-old mayor suffered gashes to his head and face, smashed teeth and shattered his right hand after responding to a grandmother's cry for help. ... Brewers RHP Dave Bush will make his scheduled start Monday after having his right arm re-examined. Bush has spent time on the disabled list and has struggled since being struck by a liner on June 4.