SAN FRANCISCO — Paul Goldschmidt broke out of a slump with two hits and two RBIs to help the Arizona Diamondbacks beat the San Francisco Giants 4-2 on Thursday night.

Martin Prado and Aaron Hill also drove in runs for the Diamondbacks, who won their second straight after losing five of their previous six. Miguel Montero added two hits.

Trevor 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.

Brad Ziegler pitched the ninth for his eighth save.

Pablo Sandoval had two hits and drove in a run for the Giants, who have lost three of four. Hector Sanchez also drove in a run and Brandon Belt had two hits.

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.

Sandoval tripled home a run and scored on Sanchez's sacrifice fly in the fourth. Diamondbacks manager Kirk Gibson was ejected on the play for arguing the call.

Gerardo Parra, who threw out two other runners on the bases, 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.

Plate umpire Tom Hallion called him safe before Sandoval tried to dive back toward home with Montero trying to beat him there.

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.

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.

NOTES: Giants C Buster Posey had an X-ray on his swollen right ring finger. Results were expected following the game. ... 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 on 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.