SAN FRANCISCO (AP) -- Didi Gregorius had a pretty good view from third base when Giants reliever Santiago Casilla's wild pitch skipped past catcher Buster Posey and went to the backstop in the 11th inning.
Arizona's speedy shortstop, who alertly went to second base earlier in the inning when outfielder Andres Torres casually fielded his one-out hit, immediately broke for the plate.
In the process, he erased the memories of a blown four-run lead and kept the Diamondbacks unbeaten in extra innings this season.
''I was getting a lead and (third base coach Matt Williams) told me to be ready in case he threw something in the dirt,'' Gregorius said after scoring the go-ahead run in Arizona's 6-4 win over San Francisco on Tuesday night. ''From the side I had a good angle, so as soon as I saw it I went. That's why I scored.''
The Diamondbacks won for just the third time in eight games but improved to 4-0 in extra-inning games despite blowing a late lead for the second time in two days.
Gregorius was hitless in four at-bats before doubling off Casilla (2-2). Pinch-hitter Alfredo Marte reached when first baseman Brandon Belt was charged with an error for bobbling a throw from third baseman Pablo Sandoval. Casilla then threw a wild pitch to Gerardo Parra that bounced to the backstop, allowing Gregorius to score.
Parra followed with an RBI double to make it 6-4.
''We made some good plays when we had to and they made a couple mistakes and we capitalized on that,'' Arizona manager Kirk Gibson said. ''We played a clean game, made good plays. We've been good at that all year.''
San Francisco got the potential tying run to the plate in the bottom of the 11th, but Matt Reynolds got Torres to ground into a double play and then retired Brandon Crawford for his first career save.
Brad Ziegler (1-0) pitched a scoreless inning for the win.
''That's the spirit of this team,'' Ziegler said. ''We'd prefer to jump out to a big lead early and kind of coast to the victory but when that doesn't happen we know we have a lot of guys on this team that are going to fight to the last out.''
The Diamondbacks, who twice blew two-run leads in Monday's 5-4 loss in the series opener, led 4-0 before the Giants scored twice in the eighth and tied it in the ninth on Belt's pinch-hit, two-run homer off closer J.J. Putz, who has blown three saves in six tries this season.
San Francisco also put two runners on in the 10th but failed to score when Sandoval was thrown out at home plate by Arizona right fielder and former teammate Cody Ross following Hunter Pence's two-out single.
''We got the hit we were looking for,'' San Francisco manager Bruce Bochy said. ''(Ross) just made the play.''
The loss ended San Francisco's nine-game winning streak against NL West opponents and stopped the Giants' seven-game streak at AT&T Park.
Arizona took a 2-0 lead against Giants starter Matt Cain in the first with some help from San Francisco's defense.
Crawford bobbled Parra's grounder to shortstop for an error and Martin Prado followed with a single. Paul Goldschmidt's looping double that landed just inside the foul line near the Diamondbacks' bullpen drove in Parra, and Prado scored on Miguel Montero's groundout.
Goldschmidt's two-run homer on a 3-1 pitch from Cain in the third put Arizona up 4-0.
Cain, an All-Star each of the past two seasons, gave up four runs in six innings and lowered his ERA from 7.15 to 6.59. He had six strikeouts and walked one but remains the only San Francisco starter without a win this season.
It's also the longest Cain has gone winless to begin a year since his third full season in 2008 when he didn't get his first win until his sixth start.
Arizona starter Patrick Corbin carried a shutout into the eighth but left with his second straight no-decision after another meltdown by the Diamondbacks' bullpen. Corbin allowed six hits, struck out seven and didn't walk a batter for the first time this season. He also doubled in the seventh for his first hit of the year.
Torres singled leading off the eighth and scored on Crawford's triple. Pinch-hitter Hector Sanchez's groundout drove in Crawford to cut Arizona's lead to 4-2.
After Angel Pagan singled, David Hernandez replaced Corbin and retired Marco Scutaro. Sandoval singled Pagan to third but Hernandez, who a night earlier gave up a tying homer to Posey, got the NL MVP to pop out softly to end the inning.
Pence doubled off Putz to open the ninth before Belt hit his first career pinch-hit homer, a towering shot into the right-field arcade.
NOTES: Giants closer Sergio Romo had not issued a walk in 38 straight appearances until Ross drew a free pass in the 10th. ... Cain has allowed six home runs over his last three starts. ... RHP Ian Kennedy (1-2) pitches for Arizona on Wednesday and is unbeaten in his last seven games against San Francisco. ... Giants LHP Madison Bumgarner, who starts the second game of the series, was winless in three games against the Diamondbacks in 2012.