San Francisco Giants' Brandon Crawford follows through with his swing after connecting for a two-run home run off Colorado Rockies starting pitcher Tyler Chatwood in the fifth inning of a baseball game Sunday, June 18, 2017, in Denver. The Rockies won 7-5. (AP Photo/David Zalubowski)

Colorado Rockies' Nolan Arenado, right, celebrates his three-run, walkoff home run with teammate Pat Valaika in the ninth inning of a baseball game against the San Francisco Giants, Sunday, June 18, 2017, in Denver. The Rockies won 7-5. (AP Photo/David Zalubowski)

San Francisco Giants pinch-hitter Hunter Pence follows the flight of his two-run home run off Colorado Rockies relief pitcher Jake McGee in the ninth inning of a baseball game Sunday, June 18, 2017, in Denver. (AP Photo/David Zalubowski)

Colorado Rockies starting pitcher Tyler Chatwood delivers a pitch to San Francisco Giants' Joe Panik in the first inning of a baseball game, Sunday, June 18, 2017, in Denver. (AP Photo/David Zalubowski)

Colorado Rockies starting pitcher Tyler Chatwood delivers a pitch to San Francisco Giants' Joe Panik in the first inning of a baseball game, Sunday, June 18, 2017, in Denver. (AP Photo/David Zalubowski)

Colorado Rockies' Nolan Arenado slides safely into third base after hitting a triple off San Francisco Giants starting pitcher Ty Blach in the first inning of a baseball game Sunday, June 18, 2017, in Denver. (AP Photo/David Zalubowski)

Colorado Rockies' Nolan Arenado celebrates as he circles the bases after hitting a three-run, walkoff home run off San Francisco Giants relief pitcher Mark Melancon in the ninth inning of a baseball game, Sunday, June 18, 2017, in Denver. The Rockies won 7-5. (AP Photo/David Zalubowski)

San Francisco Giants relief pitcher Mark Melancon follows the flight of a three-run, walkout home run that he gave up to Colorado Rockies' Nolan Arenado in the ninth inning of a baseball game, Sunday, June 18, 2017, in Denver. The Rockies won 7-5. (AP Photo/David Zalubowski)

Colorado Rockies' Nolan Arenado, front, is doused by teammates Trevor Story, back left, and Ian Desmond after hitting a walkoff three-run home run off San Francisco Giants relief pitcher Mark Melancon in the ninth inning of a baseball game Sunday, June 18, 2017, in Denver. (AP Photo/David Zalubowski)

Colorado Rockies' Nolan Arenado follows through with his swing after connecting for a triple off San Francisco Giants starting pitcher Ty Blach in the first inning of a baseball game Sunday, June 18, 2017, in Denver. (AP Photo/David Zalubowski)

Colorado Rockies' Nolan Arenado follows the flight of his three-run, home run of San Francisco Giants relief pitcher Mark Melancon in the ninth inning of a baseball game Sunday, June 18, 2017, in Denver. The Rockies won 7-5. (AP Photo/David Zalubowski)

Colorado Rockies' Nolan Arenado celebrates as his three-run, walkoff home run clears the fence off San Francisco Giants relief pitcher Mark Melancon in the ninth inning of a baseball game, Sunday, June 18, 2017, in Denver. The Rockies won 7-5. (AP Photo/David Zalubowski)

DENVER (AP) — With one dramatic swing, Nolan Arenado gave the Colorado Rockies a thrilling victory and finished off his rare feat at the plate.

Arenado completed the cycle with a three-run homer in the bottom of the ninth inning, and the Rockies stunned the San Francisco Giants by rallying for a 7-5 win Sunday.

Arenado hit the first pitch he saw from All-Star closer Mark Melancon (1-2) into the left-field seats to cap a four-game sweep. The slugger was mobbed at home plate by teammates, with a frenzied sellout crowd chanting "MVP! MVP!"

"It's probably one of the best moments in my career," Arenado said. "I've hit some big homers but by far the best, obviously, for the cycle. I've never done it. I feel like I've been kind of close but I've always needed a triple and I ain't getting that. Today is a great day."

Arenado became the fifth player in major league history to complete the cycle by hitting a game-ending home run, according to the Elias Sports Bureau. The last to accomplish the feat was teammate Carlos Gonzalez, who did it on July 31, 2010, against the Chicago Cubs.

With the best record in the National League, the surprising Rockies (46-26) have won five straight overall and nine in a row against the Giants. They are 20 games above .500 for the first time since 2009.

"The fans and the people of Denver are starting to get a hint that we're for real," Arenado said.

Colorado had a 3-2 lead in the top of the ninth but gave up three runs before scoring four in the bottom half.

Pinch-hitter Hunter Pence connected on a go-ahead homer for last-place San Francisco, which has dropped six consecutive games and nine of 11.

Melancon retired his first batter, but three singles produced a run and brought up Arenado, who tripled in the first, singled in the fourth and had an RBI double in the sixth. He hit a 91 mph fastball for his 15th homer and first career cycle.

"I was ready first pitch and I was just trying to get the ball to the outfield, really," Arenado said. "Luckily, I got a pitch middle-in and I was able to do some damage with it."

It was his second game-ending homer in the majors and the eighth cycle in club history.

Melancon has not recorded a save since May 27.

"This was as tough a one as we've had," Giants manager Bruce Bochy said. "I mean, look at those hits (in the ninth). A blooper into center field, another one that we couldn't quite get to and an eight-hopper to center. The other guy came through, but (Melancon) should have fared a lot better."

Carlos Estevez (4-0), called up from the minors before the game, struck out one batter in the ninth for the win.

Trevor Story and Pat Valaika went deep in the seventh to give Colorado a 3-2 edge after Brandon Crawford's two-run homer in the fifth put the Giants ahead.

Rockies reliever Jake McGee came on in the ninth but issued a one-out walk to Gorkys Hernandez. Pence hit an 0-1 fastball into the left-center seats for his first career pinch-hit homer, and Crawford added an RBI single later in the inning.

TRAINER'S ROOM

Giants: LHP Madison Bumgarner (left shoulder) is scheduled to throw a second simulated game in Arizona on Wednesday. He threw his first Saturday, the first time he faced hitters since getting injured in a dirt bike accident in Colorado in April.

Rockies: RHP Chad Qualls was placed on the 10-day disabled list with lower back spasms. The move is retroactive to Friday. ... Rockies RHP Chad Bettis threw 15-20 fastballs during a bullpen session, continuing his comeback from treatment for testicular cancer. Bettis, who finished chemotherapy this month, had been throwing off the mound to a catcher in front of the plate. Sunday was the first time he threw the full 60 feet, 6 inches.

DIAMOND GEM

Rockies second baseman DJ LeMahieu showed why he won a 2014 Gold Glove with a great play to save a run in the first inning. LeMahieu was moving to cover second base after Crawford broke from first when Buster Posey hit a grounder toward the vacated spot. LeMahieu reached back to field the ball and tossed it to Story while sitting on the infield. Story stepped on second to start the inning-ending double play. Crawford was initially called safe at second, but the Rockies challenged and the call was overturned.