The Giants, who have won the first two contests of this three-game set, are six games behind the NL West-leading Diamondbacks. Arizona lost to Colorado Tuesday night.
Left-hander Eric Surkamp (1-0), who was recalled from the minors to make his second major league start, allowed five hits and three runs over five innings to record his first win.
"It's awesome to think they believe in you and they have confidence in you," Surkamp said.
The Giants won their first series since August 12-14 at Florida. They had posted a 1-8-2 record in their last 11 series and were 0-4-2 in their last six sets before taking this one.
Santiago Casilla struggled in the ninth inning, allowing a pinch-hit RBI double to Anthony Rizzo. He then hit Cameron Maybin and Jason Bartlett before Giants manager Bruce Bochy summoned Ramon Ramirez from the bullpen. He fanned Jesus Guzman to end the game and notched his fourth save.
Nick Hundley homered for the Padres, losers in 11 of their last 12 games.
Wade LeBlanc (2-5) allowed eight hits and four runs over five innings to take the loss.
"We're playing hard, the results just aren't happening in the win column," Padres manager Bud Black said.
Mark DeRosa singled to start the second. Pill, a first baseman, then homered off the facade of the Western Metal Building beyond left field. He is the first Giants player to homer in his first major league at-bat since first baseman Will Clark on April 8, 1986 against Houston's Nolan Ryan.
Hundley's homer and Andy Parrino's sacrifice fly in the second tied the game, and the teams exchanged runs in the third, on a Beltran single and sac fly from Kyle Blanks.
The Giants went ahead for good in the fifth. Center fielder Justin Christian, who was called up from Triple-A Fresno and was in his first major league game since 2008 when he played for the Yankees, doubled to lead off. Beltran then doubled to the left field corner with one out for a 4-3 margin.
Beltran has collected three-or-more hits in three of four games this month, going 11-for-15 so far in September...Sandoval has hit safely in 12 of 14 games against the Padres this year, batting .358 vs. San Diego...This is just the third time in San Francisco history a player has homered in his first big league plate appearance with Johnnie LeMaster also doing so on September 2, 1975 vs. the Dodgers...The Giants have won nine of 14 meetings with San Diego this season.