SAN FRANCISCO -- Philadelphia Phillies third baseman Cody Asche has hit so poorly against left-handed pitchers this season, he was a bit surprised to see his name in the lineup Friday night in a matchup with All-Star lefty Madison Bumgarner of the San Francisco Giants.

Asche didn't do much in two at-bats against Bumgarner, but as it turns out he was simply saving his best for a key matchup with lefty reliever Jeremy Affeldt.

Asche hit a tying two-run homer in the eighth inning off Affeldt and opened a two-run 10th with a double off right-hander Santiago Casilla, eventually scoring the winning run on a hit batsman in a 5-3 victory over the Giants in the opener of a three-game series.

The defeat cost the Giants, who lost for the seventh time in their last nine home games, a chance to move past the Pittsburgh Pirates and into the second wild-card playoff position. The Pirates, who also lost Friday night, retained a half-game advantage.

"This is a tough one," Giants manager Bruce Bochy assured. "We've had a lot of them here at home. No question it's disturbing."

Employing the same game plan that worked so well in his previous at-bat, Asche opened the 10th with a double off the Casilla (2-2).

"The biggest thing was working myself into a hitters' count," noted Asche, whose liner to left-center came on a full count. "He threw me a tough slider, but I was able to work him into a fastball count, and then I knew I could be aggressive."

Left-hander Javier Lopez replaced Casilla at that point and fielded pinch-hitter Andres Blanco's bunt. But Lopez's throw to third base was too late to get Asche, with Blanco reaching safely at first.

After a flyout by center fielder Ben Revere, Lopez walked shortstop Jimmy Rollins on four pitches to load the bases. The veteran then grazed second baseman Chase Utley in the right knee with his next pitch, forcing in the tiebreaking run.

First baseman Ryan Howard followed with a sacrifice fly to deep left field, scoring Blanco with the final run of the game.

"Good comeback win," said Phillies manager Ryne Sandberg, whose team had lost two straight in Anaheim against the Los Angeles Angels to start its five-game trip.

Sandberg got a chuckle when someone noted it was a good thing Utley wears baggy pants.

"Of course, he's not going to jump out of the way of it, either," the manager praised. "That's what I like."

Right-hander Ken Giles (2-0), the second Phillies pitcher, threw scoreless eighth and ninth innings for the win. Right-handed closer Jonathan Papelbon posted his 28th save with a 1-2-3 bottom of the 10th.

The win was just Philadelphia's second in seven games against the Giants this season.

Third baseman Pablo Sandoval had three hits, including a double, for the Giants, who outhit the visitors 11-7.

The Phillies trailed 3-1 before Asche smacked a two-run home run off Affeldt with no one out in the eighth. Left fielder Darin Ruf had opened the inning with his second hit of the night, a single.

"Off the bat, I thought, 'Dang, it's going to get hung up,'" he said of his eighth homer of the season. "The wind is weird at night. Thankfully, it went over (the wall)."

Asche had only one previous home run against a left-handed pitcher all season. Affeldt hadn't allowed a homer to a left-handed hitter since April 24, 2011.

"Seeing my name in the lineup (against Bumgarner) gave me confidence," he said. "He (Sandberg) saw something in me that he wanted me in the lineup."

The Giants took a shot at reclaiming the lead in the bottom of the inning when, with two outs and Sandoval on second base, second baseman Joe Panik lined a single to right field. Sandoval was waved to the plate, but Phillies right fielder Marlon Byrd gunned him down by 30 feet.

"We're having a hard time scoring runs. We're doing all we can," Bochy said, refusing to blame third-base coach Tim Flannery for his decision to send Sandoval. "Pablo is not a speed guy. The ball was scorched. He (Byrd) made a perfect throw."

Neither starter earned a decision.

Bumgarner, who was seeking his first home win in more than a month, left the game with a 3-1 lead after seven innings. He surrendered a solo home run to Byrd in the fifth but just three other hits.

He struck out nine and did not allow a walk in his first quality start at AT&T Park since June 10.