Andrew McCutchen and Clint Barmes each hit solo home runs, and the Pittsburgh Pirates snapped a season-high seven-game losing streak with a 7-6 victory over the Chicago Cubs despite a shaky ninth-inning performance from Joel Hanrahan.

The Cubs scored twice off the Pirates closer and had the winning run on second base before Hanrahan struck out Dave Sappelt to close out the much-needed win.

Starling Marte doubled, tripled, drove in a run and scored twice for the Pirates, who were mired in a 5-17 slump since winning a 19-inning marathon in St. Louis on Aug. 19.

Wandy Rodriguez helped end the swoon, which had dropped Pittsburgh three games back in the race for the last NL wild-card spot, with a quality start. Rodriguez (11-13) scattered nine hits and three runs -- one earned -- over six innings to earn the win.

"Today I didn't have my best stuff," Rodriguez said. "I used a lot of my 2- seamer because it's the only pitch I had that I felt comfortable. When I used my curveball, I missed a lot today."

Jason Berken (0-1), making his first big-league start since Oct. 2, 2009, was touched for six runs -- just two earned -- on eight hits in four innings of work for the Cubs, who had won six of seven coming into the second meeting of this four-game series.

"He couldn't really find the strike zone," Cubs manager Dale Sveum said of Berken.

Hanrahan, in his first save opportunity since Aug. 17, put the first two hitters on in the ninth and gave up a one-out, RBI single to Alfonso Soriano to make it a 7-5 game.

Starlin Castro flew out to deep right-center for the second out before consecutive walks to Welington Castillo and Luis Valbuena forced in a run.

Hanrahan shook off the rust just in time, as he got Sappelt to swing through a 2-2 pitch to pick up his 35th save.

Marte scored on Garrett Jones' sacrifice fly in the first inning, and three straight singles to start the second, the last a line drive off the left-field wall by Rod Barajas, made it 2-0.

McCutchen took Berken deep for his 28th home run leading off the third, and after Anthony Rizzo's RBI single in the bottom half, the Pirates tacked on three runs -- all with two outs -- in the fourth.

Barmes, who reached on Josh Vitters' throwing error, scored on a triple by Marte, who trotted home on Travis Snider's base hit. Snider moved to second on McCutchen's single and crossed the plate when Jones doubled to left.

The Cubs scored twice in the fifth, one coming on Soriano's run-scoring base hit.

Alberto Cabrera served up Barmes' homer to left field in the seventh, and the Jason Grilli allowed an RBI single to Luis Valbuena in the home eighth.

Game Notes

Rodriguez has recorded a 1.83 ERA (seven earned run in 34 1/3 innings) over his last six appearances ... The Pirates had dropped 10 straight road games ... Pirates second baseman Neil Walker returned to the starting lineup for the first time since Aug. 26 and went 0-for-5. He was sidelined with lower back tightness ... Berken was claimed off waivers from the Orioles last week. He has spent the majority of this season with Triple-A Norfolk, going 5-6 with a 3.50 ERA in 26 starts.