"I kind of knew what he had," Drew said. "I was looking for something to get the ball up and put a good swing on it."
Giants reliever Dan Runzler had allowed only one base runner over 2 1/3 innings before walking Upton and Chris Young with one out in the 12th. Lopez came on for Runzler (1-2) and gave up Drew's grounder into the right-field hole on his third pitch.
"That's where I hit him where I hit him," said Arizona manager Kirk Gibson. "That's the money position right there. It might not look like it but he hates to fail as much as anybody."
Josh Collmenter (1-0) pitched two perfect innings in his major league debut for the win as the Diamondbacks snapped a four-game losing streak.
"I couldn't have dream for it to go better, to get the walk-off win and celebrate with the rest of the team," said Collmenter, who was making his first relief appearance since 2007 in his first season of pro ball.
"Just having the familiar faces was a relief to me," Collmenter said. "I wasn't facing star after star that I see on TV. To see guys that I'd faced before put things in perspective."
"Games like that it takes a little two-hopper through the hole and you get the game done," Huff said. "It was one of those tough beats but we won three series in a row and that's all you can ask for."
Upton also singled and hit his team-leading fourth home run for the Diamondbacks, who won for the third time in their last 16 games against the Giants.
Ryan Roberts tied the game at 5-5 in the eighth with a single through the hole at shortstop off Jeremy Affeldt, driving in Xavier Nady. He had earned a two-out walk off Ramon Ramirez, the first he had allowed in 9 2/3 innings across nine appearances.
"These games are tough," Gibson said. "They had their way with us the first two games. Confidence is big in this game. I'm very proud of the guys and the way they hung in there."
Diamondbacks starter Barry Enright was solid through the first five innings, allowing only Huff's homer in the fourth and fifth-inning walks to Sandoval and Brandon Belt. But Aaron Rowand, leading off the sixth, fouled a pitch off catcher Miguel Montero's groin to cause a lengthy delay. Montero stayed in the game but Enright was unable to regain his prior form.
Rowand doubled to left, went to third on Freddy Sanchez's single to left and scored on Huff's sacrifice fly to right. Posey followed with a home run into the left-field bullpen.
"That little time off threw me off a little bit but that's part of the game," Enright said. "You have to come out there and be as effective."
Enright said he had motioned to plate umpire C.B. Bucknor, asking to throw a couple warmup pitches, but Bucknor ordered Rowand into the batters' box.
Vasquez came on for Enright and surrendered a towering home run to right to Sandoval on the second pitch he threw for a 5-4 lead. It was the Giants' first back-to-back homers this season.
San Francisco starter Madison Bumgarner allowed four runs on eight hits over 6 2/3 innings with a walk and two strikeouts.
"The pitching was there," said Giants manager Bruce Bochy. "We just didn't get the hits."
Affeldt had stranded five of seven inherited runners entering the game. ... Bumgarner pitched only 1 1/3 innings less than he did his first two starts combined. ... Enright has not pitched past the sixth inning since Sept. 24, a span of four starts. ... Upton's home run carried onto the concourse beyond the left-field fence, his second to reach the concourse in six days. ... Giants LHP Barry Zito was placed on the 15-day DL with a sprain in his right mid-foot. Zito hurt it Saturday when he dove forward in front of the mound to catch a bunt. Zito will see a podiatrist in San Francisco on Monday. ... San Francisco purchased the contract of RHP Ryan Vogelsong from Triple-A Fresno to take Zito's spot. Vogelsong, who last appeared in the majors on June 22, 2006, was 2-0 with a 1.59 ERA in two starts for the Grizzlies. Vogelsong pitched for three seasons in Japan before pitching in the minors for Philadelphia and the Angels last year.