DENVER -- Josh Harrison had four hits, including a tie-breaking home run, as the Pittsburgh Pirates beat the Colorado Rockies 7-5 on Sunday.

The win enabled the Pirates to avoid being swept in the three-game series.

Harrison's homer in the eighth put Pittsburgh ahead 6-5 and came on the first pitch from Rex Brothers, who had just entered the game and gave up two homers in a four-pitch sequence. After Harrison, who went 4-for-6 with two RBIs and two runs scored, hit his fifth homer, right fielder Gregory Polanco homered on Brothers' 2-1 pitch.

Brothers (3-5) has allowed six homers in 42 innings with a 4.71 ERA this year after yielding five homers in 67 1/3 innings last year when he had a 1.74 ERA.

Mark Melancon converted his ninth straight save opportunity and earned his 19th save of the season by retiring the side in order in the ninth.

The teams had traded homers and the lead before the Pirates unloaded on Brothers in the eighth. Rockies third baseman Nolan Arenado hit his ninth homer with one out in the seventh to tie the game at 5-5. He connected off Tony Watson (7-1).

The Pirates tied the game at 4-4 in the sixth on pinch-hitter Travis Snider's two-run homer off Chad Bettis, who had given up a leadoff single to catcher Chris Stewart.

After Snider's homer, Harrison, who played both left field and third base on Sunday, singled and stole second. His head-first slide carried him past second, but Harrison scrambled to his feet and escaped a rundown to reach third and get credit for another stolen base. Tommy Kahnle relieved Bettis and struck out right fielder Gregory Polanco and center fielder Andrew McCutchen, but pinch hitter Ike Davis rifled a run-scoring double into the right-field corner on a 1-2 fastball to give the Pirates a 5-4 lead.

The Rockies capitalized on third baseman Pedro Alavarez's latest error in a three-run fourth. He moved a few steps to his left to grab a grounder from catcher Wilin Rosario and threw well wide of first base for his 21st throwing error and 23rd error overall.

Right fielder Brandon Barnes, the next batter, hit a two-run homer, his fifth homer of the season. Second baseman DJ LeMahieu followed with a single, and after pitcher Franklin Morales' sacrifice, center fielder Charlie Blackmon doubled off the fence in right-center to give the Rockies a 4-2 lead.

Morales then walked the first two batters he faced in the fifth, giving him five walks and ending his workday after 98 pitches. Bettis, pitching for the first time in five days, gave up a single to second baseman Neil Walker to load the bases. Shortstop Jordy Mercer lined to Arenado, whose dive to the base beat McCutchen for a double play. Bettis then got Alvarez to ground out.

The Pirates went ahead 2-0 in the second when Morales loaded the bases with one out on Mercer's single and walks to No. 8 hitter Stewart and pitcher Edinson Volquez. Harrison hit an RBI single, and Polanco followed with a sacrifice fly.

The Rockies got one run back in their half of the second when Rosario doubled with two outs, took third on a wild pitch and scored on LeMahieu's single.

NOTES: Rockies 1B Justin Morneau (neck strain) and LHP Boone Logan (diverticulitis) will play in a rehab game Monday at Triple-A Colorado Springs and could rejoin the Rockies on Tuesday in Chicago. Morneau is eligible to be activated that day, and Logan was eligible to return Sunday. ... Rockies SS Troy Tulowitzki (left hip flexor strain) will undergo a dry needling procedure Monday in Philadelphia from Dr. William Myers, a leg and core muscle specialist who operated on Tulowitzki's groin in 2012. ... Rockies RHP Jordan Lyles (broken left hand) is scheduled to make his second rehab start Thursday for Colorado Springs after throwing 71 pitches in 3 2/3 scoreless innings for high Class-A Modesto on Saturday night. ... Pirates 3B Pedro Alvarez was back in the lineup for the first time since Tuesday when he left the game with left knee discomfort. ... Pirates C Russell Martin, who was not in the lineup, walked five times in the first two games of the series and has reached base safely in 10 of his past 12 plate appearances.