Melky Cabrera hit his eighth homer of the season, Ryan Theriot drove in two runs and the San Francisco Giants beat the Pittsburgh Pirates 6-5 on Friday night.

Barry Zito (7-6) survived five eventful innings to win for the second time in his last six starts as the Giants snapped a three-game losing streak. Sergio Romo worked the ninth for his fifth save while subbing for regular closer Santiago Casilla.

Andrew McCutchen had three hits to raise his batting average to a National League-leading .360. Pedro Alvarez and Casey McGehee added solo homers, but couldn't stop Pittsburgh from having its four-game winning streak snapped.

Erik Bedard (4-10) endured another short outing, failing to get through the fourth inning as the Giants turned a three-run deficit into a 5-3 lead behind Cabrera's two-run homer.

The Giants have slumped since surging to the top of the National League West last week, losing five of six. The offense has sputtered while the pitching hasn't been much help. San Francisco surrendered 24 runs while getting swept in Washington earlier in the week, including a 6-5 victory Thursday in which the Giants squandered a four-run lead.

There would be no collapse Friday after San Francisco jumped on the slumping Bedard.

The veteran left-hander has been shaky over the last six weeks, battling control issues and a breaking ball that has sometimes betrayed him. He's been steady at home — coming in with a 2-2 record and a 2.31 ERA — and looked to be ready for a bounce-back performance while cruising through the first three innings.

Things unraveled quickly in the fourth.

Justin Christian led off with a walk, came home on a double by Theriot and Cabrera followed with a two-run shot to left. Buster Posey walked on four pitches and Pablo Sandoval scratched out an infield hit before Bedard could record a single out.

The Giants pushed two more runs across the plate on an RBI single by Sanchez and an error by Pittsburgh shortstop Josh Harrison that brought home Sandoval as San Francisco jumped ahead 5-3.

Zito held on to grind through five innings. He gave up four runs on seven hits, walking three and striking out three while needing 91 pitches to get 15 outs.

Unlike Thursday's meltdown against the Nationals in which San Francisco's bullpen wobbled, this time the Giants closed it out to blunt some of Pittsburgh's momentum. Romo, filling in while Casilla got the night off to let a blister on his right (pitching) hand heel, retired the side in order, getting McCutchen to ground out to third to end it and stifle the "MVP!" chants the All-Star center fielder gets every time he steps to the plate.

The Pirates have been one of baseball's biggest surprises and appeared ready to post their first five-game winning streak since September 2010 after getting to Zito early. McCutchen — who leads baseball with a whopping .456 average against lefties — knocked in Harrison with a single in the first and brought him home again in the third with a triple to left-center field.

When McCutchen trotted home on Neil Walker's double two batters later, the capacity crowd buzzed at the prospect of the Pirates climbing 11 games over .500 for the first time since the end of the 1992 season.

The Giants needed one big inning to regain control as Cabrera, who will play in his first All-Star game on Tuesday, stayed hot on McCutchen's heels in the NL batting race.

NOTES: The series continues Saturday as Pittsburgh's James McDonald (8-3, 2.45 ERA) faces San Francisco's Ryan Vogelsong (7-3, 2.26). ... Pittsburgh relievers Chris Leroux and Doug Slaten cleared waivers Friday and were optioned to Triple-A Indianapolis.