SAN FRANCISCO -- Matt Cain admitted he never expected to take a two-win season into the All-Star break.

But it sure beats a one-win first half.

The San Francisco Giants right-hander won for the first time since May 15, and right fielder Hunter Pence drove in two runs with a home run and a single, propelling the Giants to a 5-2 victory over the Oakland Athletics on Wednesday night.

The home team won for the third consecutive night in the transbay series, as the A's dominated the previous two meetings at the O.co Coliseum, 5-0 and 6-1.

Cain (2-7) was the driving force in the form reversal at AT&T Park, lifting the Giants to just a fourth win in their past 18 home games. San Francisco moved back into a tie with the Los Angeles Dodgers atop the National League West.

"If you look at how things have gone, you may find that amazing," Giants manager Bruce Bochy said of his team's standing atop the division despite recent hard times. "It's all for the good job we had done earlier. But we have to get back on track."

The Giants beat the A's for the 13th time in the past 15 head-to-heads in San Francisco.

The loss spoiled the A's debut of right-hander Jason Hammel (0-1), who was acquired in a six-player trade with the Chicago Cubs last week that also brought right-hander Jeff Samardzija to Oakland. The defeat also ended the Athletics' season-best, six-game winning streak and was Oakland's second in 11 interleague games this season.

"I thought he threw the ball pretty well," A's manager Bob Melvin said of Hammel, who had an 8-5 record with the Cubs before the trade. "This is probably not what he had hoped for, but we didn't do him any favors in the field or at the bat."

Cain gave up a solo home run to A's first baseman Stephen Vogt in the fourth inning but held the powerful Oakland lineup to just one other run in six effective innings. He struck out four.

"I felt really good," said Cain, who received more than two runs of support for just the fourth time in 15 starts this season. "The guys came up with big, clutch hits when we needed them. We kept the pressure on them. That's always a good thing."

The Giants, who scored just one run in 19 innings entering the game, gave Cain an early lead with single tallies in the second, third and fourth innings. They put some distance between themselves and the American League West leaders with a two-run sixth.

Second baseman Joe Panik and shortstop Brandon Crawford began the sixth with singles off left-handed reliever Eric O'Flaherty. After a failed sacrifice attempt by Cain, Pence increased the San Francisco lead to 4-2 with an RBI single, and Crawford raced home on a wild pitch to make it a three-run game.

The Giants bullpen closed things out from there. Left-hander Jeremy Affeldt, right-hander Sergio Romo and closer Santiago Casilla each threw one inning of no-hit relief, with Casilla logging his fourth save.

"When we're at our best, when any team is at its best, you're getting contributions throughout the lineup," said Giants catcher Buster Posey, who drove in a run with a third-inning single. "A lot of people contributed tonight."

Panik, Crawford and third baseman Pablo Sandoval joined Pence in the two-hit column for the Giants, who out-hit the A's 10-5.

Vogt and shortstop Jed Lowrie had two hits apiece for the A's, who lost their fourth in a row on the road.

Hammel struggled in his first A's start, giving up six hits and three walks in five innings. He managed to keep damage to a minimum -- three runs, two of which were earned -- by stranding six Giants baserunners, four in scoring position.

Pence's home run, his 12th of the season, came with two outs and two strikes in the fourth inning.

A fielding error by left fielder Yoenis Cespedes allowed center fielder Gregor Blanco to score from first on a hit-and-run single by Panik to open the Giants' scoring in the second. Posey's single scored first baseman Brandon Belt in the third to increase the lead to 2-0.

"We were just a little sloppy in the field, more than we usually are," Melvin noted. "It wasn't just one guy."

NOTES: Giants RF Hunter Pence is 15-for-43 (.349) with three home runs and six RBIs in 10 career games against the A's. ... A's pitchers are 0-for-28 at the plate dating back to June 3, 2013, after RHP Jason Hammel went 0-for-1. ... Hammel remained winless (0-3) in his career against the Giants in 10 appearances (nine starts). ... The Giants optioned OF Juan Perez to Triple-A Fresno after the game. They are expected to activate 2B Ehire Adrianza off the disabled list Thursday. ... Giants manager Bruce Bochy announced before the game that veteran 2B Marco Scutaro (lower-back strain) would be activated from the DL either Friday or Saturday. ... RHP Tim Hudson, a former A's All-Star, will make just his second career start against his old team in Thursday's series finale. The A's bombed Hudson for five runs in five innings while beating him when he pitched for the Atlanta Braves on May 17, 2008.