The Astros have also lost all nine of their extra-inning games this season.
"We had a chance to win," said Astros outfielder Justin Maxwell, who was at the core of the ninth-inning comeback. "It just shows a lot about this team, that we never give up."
Houston also lost shortstop and No. 3 hitter Jed Lowrie, who sprained his right ankle and injured his right knee on a collision at second base in the third inning. There was no immediate word on his prognosis. Lowrie said he underwent X-rays, but the results were not released by the club.
Brett Myers, the Astros seventh pitcher of the night, gave up an infield hit to Pablo Sandoval to start the 12th inning. Angel Pagan singled Sandoval to second. Sanchez then singled past second baseman Jose Altuve, sending Sandoval home with the winner. It was Sanchez's fourth hit of the game.
The Astros trailed 2-0 going to the ninth, having been stymied by a resurgent Tim Lincecum over eight innings.
Facing Giants closer Santiago Casilla, J.D. Martinez struck out, then Scott Moore walked. Chris Johnson struck out. Maxwell then doubled into right-center, driving in Moore to make it 2-1.
Chris Snyder then struck out on a slider in the dirt, but the ball bounced about 20 feet to the left of Sanchez. He scrambled to pick up the ball and his throw to first pulled Brandon Belt off the bag. Sanchez was charged with an error, allowing Maxwell to score from second with the tying run.
"I was actually surprised he threw it," Maxwell said. "When I hit third I was making sure he threw it, and as soon as I saw the ball in the air I took off."
The Astros' late rally denied Lincecum a badly needed victory. The Giants' two-time Cy Young Award winner had been mysteriously ineffective most of the season, carrying a startling 3-10 record and 6.42 ERA into the game. Lincecum gave up five hits -- all singles -- in eight scoreless innings. He struck out 11.
"He's still got good stuff regardless of his record," Maxwell said. "You don't win two or three Cy Youngs by being average. Hats off to him. He did a good job."
Meanwhile, Astros starter Lucas Harrell, who has mixed very good starts with shaky ones throughout his rookie season, was effective for most of his five innings. He held the Giants scoreless, matching zeroes with Lincecum, through four innings.
In the fifth, he got in trouble after he issued a leadoff walk to Lincecum.
"That's not acceptable," Harrell said. "In that situation you have to throw it down the middle and see what happens."
Harrell then walked Gregor Blanco. A double-play grounder nearly got him out of the inning, but he gave up a two-out RBI single to Melky Cabrera, the MVP of the All-Star Game earlier in the week.
Harrell walked Buster Posey, but then he struck out Sandoval to escape with no further damage. That was all for Harrell, who was finished after throwing 104 pitches.
"I felt like that was the worst I've thrown the ball all year," Harrell said. "I didn't feel good, wasn't comfortable. I wasn't locating at all today."
The Giants scored against reliever Fernando Abad in the sixth. Pagan and Sanchez began the inning with consecutive doubles.
Although the Astros lost, manager Brad Mills at least took solace in their comeback.
"I thought we did a real good job in that ninth inning to battle back like we did," Mills said.