Cain, who moved to 7-1 this year when pitching after a San Francisco loss, was making his first start since getting hit from a liner off the bat of the Padres' Tim Stauffer on July 11.
"I felt fine," Cain said. "It took me a little bit to get used to it, the first couple of hitters, a little bit. After that, it felt pretty normal. I was able to get back into that rhythm."
After scoring just an unearned run in the first 28 innings of the series, the Giants' offense erupted in the sixth against Pirates starter Zach Duke -- Cain's replacement on the National League All-Star team.
San Francisco hit four consecutive doubles to start the frame, with Sandoval's giving him the team lead with 25 on the season. It was also the club's first RBI in the series. Randy Winn, Bengie Molina and Nate Schierholtz rounded out the sixth-inning doubles.
"It felt good," Schierholtz said. "Everyone's hitting the ball hard, and it felt good to get a little bit of momentum going."
On the mound, Cain dominated the Pirates by surrendering only one run on five hits in seven innings. He also notched eight strikeouts.
The 107 pitches for Cain were the most since he threw 119 on June 14. The righty also moved to 10-0 this season when the Giants score at least three runs for him, and he won his team-high 11th game.
The only blemish for Cain came in the second, when Brandon Moss hit a home run to center. It was the first long ball the pitcher gave up since June 26.
Before getting offensive help, Cain tried to aid his own cause by hitting a ground-rule double over center fielder Andrew McCutchen's head in the fifth. It was his seventh hit and second double of the season.
Kevin Frandsen added one more double in the ninth for his first extra-base hit of the season. The Giants hit six two-baggers in total, tying their season high.
"The Giants attack first pitches very well, and you have to make quality pitches from the first pitch of the at-bat," Duke said. "I left some pitches up on the first pitch, and they hit them hard."
Preceding the four-run sixth was solid defense to keep the Giants in striking distance. In the fourth, right fielder Schierholtz corralled a Ryan Doumit liner and fired to Edgar Renteria. The shortstop dove and applied the tag to stop Doumit from turning a single into a double.
Handed a three-run lead, the usually dominant Giants bullpen had a rare rough outing in the eighth. The two batters Sergio Romo faced both hit singles and later scored on a Doumit single off Jeremy Affeldt, who came in for relief. Romo also dealt with what he called a stinger, as Bochy said the reliever got his spike caught. But the reliever said after the game that he was fine.
After the two runs, Affeldt rebounded against the next batter by getting Adam LaRoche to hit into an inning-ending double play on a 3-0 count. Affeldt, who entered Sunday with the Majors' longest active scoreless streak at 23 innings, hasn't been charged with a run since May 7.
"I just tried to kind of keep it in and off on his hands, where I could maybe try to keep it off his barrel," Affeldt said. "It just happened to work out. I knew he was going to be aggressive, but it was a pitch that I didn't want to just give in."
Brian Wilson then closed the game with a scoreless ninth for his 24th save of the season.