Sanchez's sharp grounder bounced off the glove of Houston second baseman Jose Altuve and went into right field, allowing Pablo Sandoval to score from second base and lift the Giants to a 3-2 win over the Astros on Saturday night.

"That's what we have to do," said Sanchez, who finished with a career-high four hits. "We have to keep working, keep playing and never give up. It felt amazing to get that hit."

Sanchez was on the hook to be the scapegoat before his second walkoff hit of the season lifted the Giants to their second straight win over the Astros.

Nursing a one-run lead with the tying run at second and two outs in the ninth, San Francisco closer Santiago Casilla threw a wild pitch that bounced away from Sanchez on a swinging third strike by Houston's Chris Snyder. The Giants backup catcher quickly retrieved the ball, but threw wide of first base for an error, allowing Justin Maxwell to score.

"I tried to go too quick on that play," Sanchez said. "I had a lot of time ... but that's what happens when you try to do too much."

Sandoval singled leading off the 12th against Brett Myers (0-4), Houston's seventh pitcher. Angel Pagan then singled before Sanchez delivered the game-winner.

"It is rewarding to see a guy who felt so awful a few innings earlier come through," San Francisco manager Bruce Bochy said.

Melky Cabrera had two hits to add to his majors-leading total for the Giants, who moved into first place in the NL West, a half-game ahead of the Los Angeles Dodgers.

Jeremy Affeldt (1-1) got the last out in the top of the 12th for the win.

Casilla gave up both Houston runs in the ninth, preventing Lincecum from earning the win after his most impressive outing of the season.

The two-time NL Cy Young Award winner, who went into the game with the highest ERA in the majors among qualifying pitchers, allowed five hits over eight innings and had a career-high 11 strikeouts.

There had been talk before the game that San Francisco might have its struggling ace skip a start, but that probably won't happen now after Lincecum's sharpest outing of the season.

"I'm going to take the small steps as they come," Lincecum said. "Even though we won I'm going to go home and reflect on this and think about the things I did well and try to duplicate them in my next start."

It's only the second time this season that Lincecum has gone eight innings and it dropped his ERA fell to 5.93, still the highest of his career.

It was a tough evening all the way around for the Astros.

Houston fell to 9-34 on the road and lost shortstop Jed Lowrie to a sprained right ankle.

"I thought we did a real good job in the ninth inning, to be able to battle back like we did," Astros manager Brad Mills said. "Our bullpen battled their way through and they were able to get through to Myers in the 12th."

The Astros didn't do much at all until their two-run rally in the ninth.

Casilla struck out J.D. Martinez to open the ninth then walked Scott Moore. After another strikeout, Justin Maxwell doubled in Moore and later scored from second base on the wild pitch by Casilla and throwing error by Sanchez.

Astros starter Lucas Harrell worked out of a pair of jams early before walking Lincecum and Gregor Blanco leading off the fifth. Harrell got Ryan Theriot to hit into a double play but Cabrera followed with single to right that drove in Lincecum.

Pagan and Sanchez greeted Houston reliever Fernando Abad with back-to-back doubles in the sixth to make it 2-0.

Game notes
Cabrera has 40 multi-hit games this season, tops in the majors. ... Houston catcher Jason Castro has been bothered by pain in his right knee but Mills is hopeful of starting Castro in Sunday's series finale. Houston is being cautious because it's the same knee on which Castro had season-ending surgery in March 2011. ... Sanchez started at catcher for San Francisco, his fifth straight when Lincecum is on the mound. Sanchez has also caught all 17 starts by LHP Barry Zito, but Giants manager Bruce Bochy said All-Star catcher Buster Posey will be behind the plate for Zito's next start Tuesday in Atlanta. Posey started at first base against the Astros. ... RHP Matt Cain pitches the finale for the Giants, the first time he's faced the Astros since pitching a perfect game against them on June 13. .. The start time for San Francisco's July 28 home game against the Dodgers has been changed from 6:05 p.m. to 1 p.m. PDT to accommodate television.