Solo homers by Garrett Jones -- the 28th splashdown by a visiting player into McCovey Cove -- and Jordy Mercer off Cain preceded a seven-run outburst in the fifth against Giants relievers highlighted by Pedro Alvarez's double for two runs and Jose Tabata's for three more.
Not laughing was Jeff Locke. The left-hander had a 10-3 lead -- had even contributed to it with a single in the middle of the seven-run explosion -- and did not get to pitch the fifth inning that would have qualified him for his 10th win.
That odd development was a cautionary note sounded in Pittsburgh's 75th win, as the Pirates' lone All-Star starting pitcher remained stuck on one win in seven second-half starts. Both Locke and manager Clint Hurdle confirmed that the pitcher had been told his night was over even before that fifth inning had begun.
With Locke having been hit hard and fighting his control through four, the Pirates had Jeanmar Gomez warming up as that put-away rally began, thus were already committed to having him replace Locke.
"If Locke doesn't get the last out in the fourth, there's a chance Gomez comes in then," Hurdle said.
"Sure, it's a little frustrating when you see what happened after that," Locke said. "I kind of thought maybe there was a possibility I'd go back out there, but the decision was made when it was 3-3, not 10-3. Gomez was loose, and there was no sense having him sit down and waste an arm."
Gomez's arm certainly did not go to waste, as the right-hander provided a dramatic contrast to Locke's struggles. In three hitless innings, he issued one walk and stayed consistently ahead of hitters by throwing 26 strikes among 39 pitches to earn his third win in as many decisions.
So, as the antidote to the rare flat-out off night by one of their starters, the Bucs unleashed a 13-hit attack to pick up a member of the rotation who had been picking them up all season.
And most of the Bucs' offensive damage came against the San Francisco bullpen pressed into early duty after Cain was knocked out of a still-competitive game by Gaby Sanchez's lined shot off his right forearm in the top of the fourth. X-rays indicated Cain got away with only a bruise, but it gave the Bucs an opening they exploited.
"We believe in us," Hurdle said. "We don't always know how it's going to happen. We were already having a good night off a tough pitcher, and after that did some really good hitting with runners on base."
Alvarez snapped a 3-3 tie with his double off lefty Jose Mijares that bounced over the fence, tying his career high of 85 RBIs while torching the big fifth in which even the first two outs produced runs (sacrifice flies by Russell Martin and Sanchez) and which was punctuated by Tabata's three-run double.
Working for the NL West Giants, Cain seldom sees the Pirates, and the pleasure tends to be all his. The last time Cain faced the Bucs, on June 13 in Pittsburgh, he blanked them on two hits for 6 2/3 innings. The time prior to that, on April 13, 2012, he came within a sixth-inning James McDonald single of a perfect game against them. So this latest encounter already wasn't going typically well.
Cain trailed, 3-1, in the second after the homers by Jones and Mercer.
"I made some bad pitches with fastballs and they didn't miss them," Cain said.
Jones connected with one of them for his 99th career homer -- which snapped an 0-for-19 drought.
"That was just being on time on his best fastball," Jones said of Cain's 3-2 offering, "and letting the hands do all the work. I wasn't trying to hit a home run by any means, just trying to put a nice short swing on it, and I got a good part of the bat on it."
Little could Cain have known those would be the Pirates' least damaging blows of the night against him.
Pittsburgh's uncharacteristic offensive eruption came on a night Locke further dimmed the memory of the brilliant first half that made him an All-Star. In addition to five hits and numerous smashed outs in four innings, he walked four to take the NL lead with 72 bases on balls. Three of the walks led off innings.
"I'm throwing on the corners a lot more. I definitely don't want to miss in the middle," said Locke, whose postgame mood was considerably brightened by the punches delivered by his mates.
"He was the happiest guy in the clubhouse that we battled back and were able to win a ballgame," Hurdle said.
The third inning could have been a turning point. In the top, an Andrew McCutchen single and Alvarez's first double -- an easy score prevented by the ball bouncing into the stands -- placed Pirates at second and third with one out. But McCutchen was nabbed at the plate by Cain's quick reaction on a ball topped back to the mound by Martin, then Jones' infield pop ended the inning.
The Giants came out hitting lasers in the bottom of the inning -- yet managed only one run, on a Buster Posey sacrifice fly.
The one time Locke broke off a nasty two-seamer, following another leadoff walk of Pablo Sandoval in the fourth inning, it backfired on him: Brett Pill topped it a few feet down the third-base foul line, a perfect swinging bunt that resulted in two men on with none out. After Gregor Blanco bounced into a force and joined Sandoval in scoring position on Guillermo Moscoso's sacrifice bunt, both scored on Marco Scutaro's sharp single to center to briefly tie it at 3.
Jared Hughes allowed a two-run double to Brandon Crawford in the eighth, then Tony Watson took over to end the game by retiring all four men he faced.