SAN FRANCISCO (AP) — Andrew McCutcheon had two hits, including a two-run homer, pinch-hitter Matt Joyce had a key two-run single, and the Pittsburgh Pirates rallied from a four-run deficit to complete a three-game sweep of the freefalling San Francisco Giants with a 6-5 victory on Wednesday.

The Pirates won their fourth straight game and sixth in seven. They remain a game behind St. Louis, which beat Houston 8-2, in the race for the NL wild card.

READ MORE: Contractor Who Bribed San Francisco Public Works Director Sentenced to 2 Years in Prison

The Giants, who on Tuesday fell out of first place in the NL West for the first time in 97 days, lost their fourth straight. They fell to 9-21, the worst record in the majors since the All-Star break. They dropped a full game behind the Dodgers, who play Philadelphia tonight.

McCutcheon’s homer, his 17th, capped a six-run rally in the fifth inning that chased Giants starter Matt Cain (4-8).

Cain had allowed one hit and had faced just one batter over the minimum with a 4-0 lead going into his disastrous fifth inning, when the Pirates scored six runs on four walks, three hits and a hit batsman.

His first pitch to McCutcheon was an 89 mph fastball over the middle of the plate, and the Pirates center fielder pounced.

Pirates starter Ivan Nova allowed four runs and six hits in four innings. He struck out five and allowed no walks. Nova, who won his first two starts since coming over in a deadline trade with the New York Yankees, was lifted for Joyce after Cain hit David Freese to start the fifth and then walked three straight batters.

READ MORE: Warnings About San Francisco Millennium Tower Repair Plans Raised Before Work Began

Jeff Locke (9-7) pitched a scoreless fifth.

Tony Watson survived a scoreless ninth after the Giants loaded the bases with no outs for his seventh save in eight tries since taking over Pittsburgh’s closer’s role.

After inducing Buster Posey to ground into a run-scoring double play, Watson got Brandon Crawford to fly out to shallow center and McCutcheon made a game-ending running catch.

Posey was 3 for 5 with an RBI double to lead the Giants. Crawford was 2 for 5 with a double, a triple and three RBIs and Angel Pagan was 3 for 4 with a double as he extended his hitting streak to 15 games.

TRAINER’S ROOM

Pirates: RHP Tyler Glasnow, on the DL since July 24 with right shoulder discomfort, threw a bullpen session on Tuesday and is showing progress, manager Clint Hurdle said. … C Chris Stewart, out since July 2 with knee discomfort, had his third scheduled rehab game with Double-A Altoona rained out on Tuesday.

MORE NEWS: Video Released of Suspect In Assault Pro-Recall Supporter in San Rafael

Giants: Manager Bruce Bochy had Posey in the lineup for a day game after a night game after the All-Star catcher missed two consecutive games with lower back tightness. “I don’t want him going out there if there’s any risk of making it worse, he says he’s fine,” Bochy said. … 1B Brandon Belt dropped to the ground after fouling a pitch off his right knee in the fifth inning but stayed in the game.