Hunter Pence hit a grand slam, Matt Cain dodged trouble for five-plus innings and the San Francisco Giants beat the Arizona Diamondbacks 6-2 on Friday night.

Cain (14-5) walked three in the first and stranded five runners over the first two innings. He failed to make it through six innings for the second time in his last three starts.

The right-hander allowed four hits, walked a season-high five and also hit a batter. He was charged with one run and struck out two.

Buster Posey and reliever Santiago Casilla each drove in a run for the NL West-leading Giants, who won for the third time in four games and are 11-5 in their past 16.

Chris Johnson drove in both runs for the Diamondbacks. Tyler Skaggs (1-2) allowed five runs and five hits in four innings after lasting only three innings in his previous start against San Diego on Sept. 7.

Cain loaded the bases in the first and allowed the first two runners to each in the second but escaped without surrendering a run.

Skaggs got into trouble in the third, and the Giants capitalized.

Cain had a leadoff walk and moved to second on Marco Scutaro's one-out infield single. After Pablo Sandoval singled to center to load the bases, Buster Posey hit a grounder to third and Cain slid under Dan Wheeler's throw on a close play to make it 1-0.

Pence followed with a long drive into the left-field stands for his fourth homer since he was acquired from Philadelphia at the trade deadline. It was No. 21 overall for the season.

The Diamondbacks finally broke through in the sixth. Cain hit Miguel Montero in the foot and Justin Upton singled to right. Cain then gave way to Jose Mijares, who yielded an RBI single to right by pinch-hitter Chris Johnson that made it 5-1.

Guillermo Mota came on and hit John McDonald in the left shoulder to load the bases before retiring Chris Young on a line drive caught by a diving Sandoval at third for the first out of the inning.

Jeremy Affeldt, the Giants' fourth pitcher in the inning, got Adam Eaton to ground into an easy force at home and struck out Aaron Hill to end the threat.

Casilla threaded a bases-loaded single into right field in the eighth for his first career hit, putting the Giants ahead 6-1.

Johnson added an RBI single in the ninth for Arizona.

NOTES: Pence has three career grand slams, the previous one coming May 20, 2008. . Cain threw 34 pitches in the first inning and 106 overall. . The roof at Chase Field was open during the game for the first time since June 9. . Diamondbacks LF Jason Kubel doubled off Angel Pagan at first in the seventh for his 13th outfield assist of the season, a new club record. . Montero, who walked three times and was hit by a pitch, became the second Diamondbacks player to reach base four times without a hit in a nine-inning game. Luis Gonzalez accomplished the feat twice. . Arizona's Double-A Mobile affiliate won its second straight Southern League championship series title Friday night and its rookie-level team in Missoula won the Pioneer League championship series. . LHP Wade Miley, the second of three straight rookie southpaws, will take the mound for the Diamondbacks against veteran LHP Barry Zito. Zito is 5-8 in 19 career games against Arizona but 2-0 in three starts this year. . The Diamondbacks will stage their first-ever alumni game after Saturday's game.