Tyler Skaggs' inexperience was on display Friday night.

"He is very young," Arizona manager Kirk Gibson said after the left-hander lasted just four innings in a 6-2 loss to the San Francisco Giants. "You look at all the 21-year-olds who have been in the game over the history of the game, they learn a lot of lessons. It is painful and tough for him."

Skaggs (1-2) allowed five runs and five hits after lasting only three innings in his previous start against San Diego on Sept. 7. He also failed to cover first on a ground ball that took an odd hop off the edge of the first-base bag in the second, though the miscue didn't come back to hurt him.

"That was just a mental error," Gibson said. "He's young. He thought the ball was going to go foul and he didn't get over there. He will learn from that. The result isn't what he wants it to be but your hope is he will have the character to make him better. If we didn't believe that, I don't think he would be here."

Skaggs retired the side in the first and fourth but gave up five runs in the third, the bulk coming on Hunter Pence's third career grand slam.

"It's been unacceptable on my part the last two outings," Skaggs said. "It's almost been embarrassing. As a young pitcher you have to learn to get out of jams like that. I was fortunate my first two outings to get out of jams."

Pitcher Matt Cain sparked the outburst in the third with 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 Ryan 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.

"You can never walk the pitcher," Skaggs said. "I thought they were good pitches but I ended up walking him and then the whole inning kind of exploded."

Chris Johnson drove in both runs for the Diamondbacks, who stayed 4½ games behind the Cardinals for the NL's second wild-card spot.

Arizona left 15 men on base against seven Giants pitchers.

Cain (14-5) walked three in the first and stranded five runners over the first two innings.

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

Cain failed to make it through six innings for the second time in his last three starts.

"I couldn't put away some guys like I wanted to," Cain said. "I got too fine with some pitches."

After putting the first two runners on three times in the first five innings, 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 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.

Reliever Santiago Casilla also drove in a run on his first career hit for the NL West-leading Giants, who remained 7½ games ahead of Los Angeles with 18 games remaining. The Dodgers beat St. Louis 8-5 on Friday night.

Johnson also had an RBI single in the ninth for Arizona.

NOTES: Sandoval, returning to the lineup after two days off, finished 3 for 4. . 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.