PHOENIX -- The San Francisco Giants may have been a bit weary after an overnight flight from Florida.
Pablo Sandoval provided the equivalent of a double shot of espresso.
Sandoval's towering two-run homer off Billy Buckner (2-2) in the fifth gave the Giants a 5-4 lead, and they didn't trail again.
"Man, he can flat-out hit," Cain said of Sandoval, who raised his average to .318. "It's amazing."
Cain (8-1) won his fifth straight start and pulled into a tie for the NL lead in victories with Colorado's Jason Marquis, Johan Santana of the New York Mets and Chad Billingsley of the Los Angeles Dodgers.
But Cain wasn't as dominant as he has been for much of the year, giving up four runs on six hits and five walks in 6 1/3 innings. Cain struck out six.
Cain won because his teammates relocated their bats 24 hours after being shut out by the Marlins in Florida. Juan Uribe had a homer and two doubles and Edgar Renteria added three hits for the Giants, who had a season-high 17 hits.
"The bats came alive," San Francisco manager Bruce Bochy said. "Sometimes we're going to have to win games where we slug it with the other club."
"Those [errors] are unacceptable," Arizona manager A.J. Hinch said. "We're going to address it."
Arizona was 12-17 when Hinch replaced Bob Melvin as manager on May 8, and the Diamondbacks are 13-17 since that move.
Cain entered with a 2.27 ERA, second in the NL behind Santana. But he struggled with his command on a 92-degree night in the desert.
"I had the right ideas with what I wanted to do to try to get them out, and I was obviously getting behind in counts," Cain said.
After Uribe homered in the second to give the Giants a 1-0 lead, Cain gave up a tying leadoff homer to Reynolds in the bottom of the inning -- the first homer Cain had allowed since May 12. Two singles and an RBI sacrifice bunt by Buckner put the Diamondbacks up 2-1.
Sandoval's single in the third tied it at 2-2. But Cain opened the third with a walk to Justin Upton and then surrendered a two-run homer to Drew, and the Giants suddenly trailed 4-2.
Bochy said that Cain might have come undone in the past. Instead, he bore down and kept his team in the game.
"I think tonight demonstrated, really, how much he's grown as a pitcher," Bochy said. "He could have caved in. It wasn't going his way. He gave up four runs, we got down, but he gave us a gutty effort and battled out there as well as you can."
Cain's persistence paid off when San Francisco rallied against Buckner with three runs in the fifth and three in the sixth.
"Three-run innings are hard to swallow, and they had three-run innings back-to-back," Hinch said. "It's a punch in the gut, and that was the game."
In the fifth, San Francisco scored on an RBI single by Randy Winn and a 433-foot shot to right by Sandoval, his fifth.
Sandoval is 14-for-29 (.483) against Arizona this season with two homers, two doubles, a triple and six RBIs.
In the sixth, Uribe led off with a double, then took third and scored on wild pitches by Buckner. RBI singles by Aaron Rowand and Renteria gave San Francisco an 8-4 lead.
After allowing a total of six earned runs and 16 hits in his first three starts, Buckner gave up eight earned runs and 13 hits in 5 2/3 innings. He struck out five and threw two wild pitches.
"I felt fine," Buckner said. "Obviously execution wasn't great. I left a few balls over the plate and they were able to take advantage of that."
Arizona RHP Brandon Webb will begin throwing off a mound soon, but the club has no timetable for his return to the rotation. Webb, who has tightness in his right shoulder, has made only one start. ... Diamondbacks LHP Scott Schoeneweis returned to the active roster 21 days after the death of his wife, Gabrielle.