The Pirates had won the first two outings in the three-game series, but they managed just three hits against four San Francisco pitchers.
San Francisco broke open the game with a five-run sixth inning featuring a three-run homer by Hunter Pence. Gregor Blanco added an RBI single in the sixth after driving in a run with a base hit in the second.
Cain (5-3) gave up two hits and walked two to win for the second time in his last five starts.
The loss went to Charlie Morton (0-1), who made his season debut after opening the year on the disabled list following elbow surgery. This was his first major league appearance since May of 2012.
Morton allowed two runs on seven hits in five innings.
Moore to attend retreat in to avoid Kutcher's wedding
Yosemite climber falls 30 feet, suffers major injuries