Updated

The Pirates showed up in San Francisco mired in a 10-game losing streak with little going right. In two weeks, they'd fallen from first place to 10 games out.

Winning two from the reigning World Series champion Giants did a lot as Pittsburgh heads off to face NL Central-leading Milwaukee in an important matchup of division rivals.

Andrew McCutchen hit a two-run homer, Jeff Karstens bounced back from his worst start of the season and the Pirates won their first series in three weeks with a 9-2 victory over the struggling Giants on Wednesday.

Pittsburgh is off Thursday before opening a weekend series with the Brewers, who have won all five previous meetings.

"We'll find out after the Friday night game, but we're going in with some confidence," Pirates manager Clint Hurdle said. "We were able to put a foot down and win a series on the road."

Garrett Jones added an RBI single to continue his torrid August and help back Karstens (9-6), whose career-high nine strikeouts matched a season high for the team. Karstens, who ended a four-start winless stretch, gave up a season-high nine runs and nine hits in a 15-5 loss at San Diego last Friday.

"It's good for us, especially heading into Milwaukee," Karstens said.

Pablo Sandoval homered for the Giants, who fell a half-game behind Arizona in the NL West. San Francisco had led the division since June 25.

Jonathan Sanchez (4-7) walked McCutchen on four pitches and threw five straight balls to start the game. Sandoval came in from third and catcher Eli Whiteside hustled to the mound to chat with the left-hander, who was roughed up again in his second start since coming off the disabled list with biceps tendinitis. Sanchez was done after 4 1-3 innings and hasn't won since June 2 at St. Louis.

McCutchen went 1 for 1 with three walks and a hit by pitch. He stole two bases and scored four runs — most by a Pirate in a game this year. His 16th clout of the year put Pittsburgh ahead 3-1 in the third. The Pirates have homered in four of their last five games and eight of 12.

McCutchen was hit by a pitch leading off the seventh. Brandon Wood drove in a run that inning and Matt Diaz hit two run-scoring singles. Pittsburgh's Nos. 1-5 hitters went a combined 7 for 17 with six RBIs, eight runs scored, seven walks and a hit batter. Jones is batting .389 (14 for 36) with five doubles, three homers and seven RBIs this month.

With a power show on offense of late, Bucs pitchers also have allowed 18 homers over the last nine games. Sandoval connected in the bottom of the first to tie the game at 1.

The Giants have hit 19 straight solo home runs, matching the 1914 Phillies' major league mark. Their last homer to score more than one run was a two-run drive by Nate Schierholtz on July 6 against San Diego.

San Francisco finished up a disappointing 3-7 homestand with its 10th loss in 13 games. The Giants have dropped three straight series at AT&T Park for the first time since losing six consecutive home series from May 12-June 18, 2008.

"We've got a lot of baseball left," outfielder Cody Ross said. "We've got to go out and play like champions. We're not doing it. We're kind of all in a rut and it seems like we can't dig ourselves out. We've got to do something."

Giants manager Bruce Bochy said the front office would discuss whether to stick with Sanchez. Lefty Barry Zito had been unimpressive in the fifth starter role and went back on the disabled list Aug. 1, but he could come off when San Francisco needs that pitcher again Aug. 16 in Atlanta.

"It has me a little concerned," Bochy said of the No. 5 spot. "We need that guy to help us win a ballgame, too. We haven't had a good start there in I don't know how many now."

Pirates first baseman Derrek Lee was a late scratch from the lineup with a recurring left hand injury. Hurdle said Lee's hand was very sore after he played the two previous nights following a four-game absence with the bruised hand. Lee was hit with a pitch from Chicago's Carlos Marmol last Wednesday.

Lee is batting .278 with two home runs and three RBIs in four games since joining the Pirates in a trade from Baltimore.

NOTES: Giants RF Carlos Beltran missed his third straight game nursing a strained right hand and wrist. Schierholtz was held out with a right hip flexor. ... Zito will make another rehab outing Saturday for Triple-A Fresno then fly out to join the Giants on the road. ... San Francisco INF Miguel Tejada (lower abdominal strain) isn't quite ready to begin a rehab outing in the minors, Bochy said. Tejada will travel on the road trip. ... Pirates RHP Kevin Hart had arthroscopic cleanup surgery Tuesday performed by Dr. James Andrews in Pensacola, Fla. The team hopes to have Hart ready by spring training. ... Pirates LHP Paul Maholm (6-12, 3.54 ERA) starts Friday trying to end a string of three straight losing decisions. He is 1-5 on the road and is coming off a 13-2 loss Saturday at San Diego. ... All-Star RHP Matt Cain goes for the Giants on Friday at Florida, trying to improve to 4-1 for his career vs. the Marlins. He lost to Florida on May 24.