Cahill (6-10) won his third straight decision since coming off the disabled list. He allowed two runs and six hits in five innings, walking four and striking out three.
"It's kind of like I have one bad inning where I lose it," Cahill said. "I got out of it with help from the right fielder."
Gerardo Parra, the right fielder, threw out two Giants on the basepaths and Diamondbacks manager Kirk Gibson was ejected from the game for arguing for a third one.
"He was feeling good tonight," Gibson said of Parra. "He's accurate, he's made himself better and he's a dominant player out there."
Brad Ziegler pitched the ninth for his eighth save.
Ryan Vogelsong (3-5) lost for the first time since May 15, though he missed 69 games with a fractured right hand. He gave up four runs and eight hits over 4 2/3 innings, walking three and striking out three.
"Vogelsong struggles the second and third time through the lineup," Gibson said. "It looked like he just lost it."
Sandoval tripled home a run and scored on Sanchez's sacrifice fly in the fourth. Gibson was ejected on the play for arguing the call.
Parra, who leads the NL with 13 outfield assists, made a strong throw that appeared to beat Sandoval. Miguel Montero held onto the ball as Sandoval crashed into him, sending both players sprawling on the ground.
"I thought he missed it," Gibson said.
Plate umpire Tom Hallion called him safe before Sandoval tried to dive back toward home with Montero trying to beat him there.
"There's not a better arm in baseball," Giants manager Bruce Bochy said. "He's as accurate as any outfielder. He's electric and showed it tonight. He helped win the ballgame for them. That's how good he is."
Vogelsong retired the first two batters in the fifth and then didn't get another out, giving up four runs and five hits with a walk before he was replaced by Jake Dunning.
"He just lost it," Bochy said. "He had two outs and nobody on and he had trouble making a pitch there. He got the ball up and made some mistakes. It got away from him."
Goldschmidt, who was hitless in 16 previous at-bats, doubled home two runs. Prado followed with an RBI double and Hill followed with an RBI single.
Giants C Buster Posey had an X-ray on his swollen right ring finger. ... LHP Jeremy Affledt went through fielding drills and will throw 40 pitches to hitters on Friday. ... RHP Chad Gaudin threw a bullpen session and is closer to returning. ... RHP Yusmeiro Petit will start Friday's game for the Giants. He's won both of his previous two starts. ... LHP Patrick Corbin starts Friday for the Diamondbacks. He's 2-2 in seven career starts against SF. ... LHP Matt Reynolds (partially torn UCL in left elbow) threw a bullpen session and could return soon. ... RHP J.J. Putz remains in Arizona for his rehab from a dislocated right pinkie finger. He's playing catch with tape on his fingers. ... Arizona SS Willie Bloomquist will get a day off Friday. ... Vogelsong has 97 strikeouts in the first inning, his best of any inning. ... Giants C Johnny Monell made his major league debut, popping out to shortstop to end the game.