Matt Carpenter returned to his comfort zone Thursday night.

Carpenter hit a pair of home runs and Greg Garcia’s bases-loaded walk capped a three-run rally in the ninth inning as the St. Louis Cardinals beat the Colorado Rockies 9-8.

Carpenter went 4 for 5 with four RBIs after moving back to the leadoff spot. It was his first career multihomer game and he has a career-high 12 home runs this season.

“I’d be lying if I said I wasn’t comfortable there,” Carpenter said. “It’s not to say that I can’t hit in other positions. Today might have bought myself a couple of more games there.”

Carpenter is hitting .370 (34 for 92) with six homers and 17 RBIs in the leadoff position. He hit .225 (56 for 249) with six homers and 33 RBIs in the 2-hole.

“Leadoff spot fit him well today,” manager Mike Matheny said. “What a great day. Even the out he made was hit on the screws. That just shows if you stay with it, it’s going to come around. That was just a great display of everything we saw in the first part of the season.”

Jhonny Peralta’s two-run single off Rockies closer John Axford tied the game at 8-all and Garcia’s walk scored Kolten Wong as the Cardinals recorded their seventh walk-off win of the season.

Axford (3-5) took his third loss in his last four outings.

“We knew Axford had been going through a rough patch,” Carpenter said. “We just wanted to get the tying run to the plate and see what happened.”

Cardinals starter Carlos Martinez gave up five runs on 10 hits in five innings. Carlos Villanueva (4-3) earned the win.

The Cardinals snapped a two-game skid and improved to a major league-best 38-15 at home.

The Rockies are 3-15 at Busch Stadium since 2010 and haven’t won in St. Louis since May 12, 2013.

The Cardinals ended a 22-inning scoreless streak when Wong’s groundout scored Carpenter in the first.

Carpenter’s 423-foot homer to dead center in the second gave the Cardinals a 4-1 lead and his solo shot in the fifth tied it at 5.

Nick Hundley’s 442-foot home run to left in the sixth tied the game at 6. The Rockies capitalized on a pair of throwing errors by Kevin Siegrist to snap that tie with two runs in the eighth.

Nolan Arenado drove in his major league-leading 78th run in the first.

“We battled really well offensively,” Rockies manager Walt Weiss said. “We did in Chicago too the last series. I think we’ve linked together a lot of good at-bats, we did it again tonight.”

Rockies starter Chris Rusin erased one of his mistakes to Carpenter by hitting his first major league home run to tie the game in the fourth. Rusin, who gave up six runs in five innings, became the third opposing pitcher to go deep against Cardinals pitching this season.