DENVER — The cavalry waited until the ninth inning, but it eventually arrived to work some Rocky Mountain magic Wednesday.

“We still have an outside shot,” Pete Alonso said, referring to the postseason, after the Mets scored four runs in the ninth to beat the Rockies 7-4 at Coors Field. “If keep winning games and control what we can control, I think good things are going to happen.”

Alonso drew a bases-loaded walk against Joe Harvey to give the Mets the lead in the ninth after Brandon Nimmo had tied the game with an RBI single moments earlier. In the NL wild-card race, the Mets moved within three lengths of the Cubs and Brewers — both of whom lost Wednesday — before departing for Cincinnati, where they open a weekend series Friday. The Mets have 10 games remaining.

Jeurys Familia, Brad Brach and Seth Lugo combined for 3 ¹/₃ innings of scoreless relief after Noah Syndergaard sputtered in a third straight start. Lugo recorded the final six outs and also delivered an RBI single that gave the Mets their final run.

Rene Rivera’s presence behind the plate not only failed to resurrect Syndergaard, it gave manager Mickey Callaway an extra decision to make that almost worked against his team.

The light-hitting Rivera — despite a full bench that included Wilson Ramos, J.D. Davis and Joe Panik — stayed in to bat with the bases loaded in the sixth inning and was retired for the final out in a game the Mets trailed by a run. Callaway was exonerated for the curious decision to let Rivera bat after Ramos and Davis reached base to begin the go-ahead rally in the ninth.

Callaway later defended his decision to let Rivera bat in the sixth inning.

“The main thing was Noah was still probably going to pitch there,” Callaway said. “That was the main thing, and Rivera is a good hitter. He hit 25 homers [in the minor leagues] and he puts good swings on the ball.”

Syndergaard had previously lobbied for somebody other than Ramos to catch him, and received his wish, with the veteran Rivera inserted into the starting lineup. But Syndergaard still struggled, allowing four earned runs on 10 hits and two walks over 5 ²/₃ innings. It was a third straight start in which the right-hander allowed four runs without completing six innings.

“I thought it was pretty disappointing overall,” Syndergaard said of his performance. “The boys really bailed me out.”

Michael Conforto’s RBI single in the eighth pulled the Mets within 4-3, but Amed Rosario struck out against Jairo Diaz with the tying run at second base to end the rally.

Alonso blasted homer No. 49, a solo shot in the sixth, that sliced the Rockies’ lead to 3-2, but the Mets missed a huge opportunity in the inning. After loading the bases with one out, Rosario was retired on a pop up and Rivera remained in the game to bat. Rivera fell into an 0-2 hole before working the count full and grounding out to end the threat.

Alonso’s homer was the Mets’ 225th of the season, setting a franchise record that previously had been established in 2017. The Mets began the day fifth in the National League in homers.

“[Coors Field] is a great place to hit and I love the background,” said Alonso, who blasted a monstrous homer the previous night. “The stadium is great. The city of Denver is great. It’s a pretty cool road trip so far.”

Syndergaard loaded the bases with nobody out in the fifth, but escaped with only one run scoring, on Charlie Blackmon’s sacrifice fly that gave the Rockies a 3-1 lead. Pitcher Jeff Hoffman bunted for a single to begin the rally and Trevor Story singled before Syndergaard walked Ryan McMahon to load the bases.

Sam Hilliard’s second homer of the game put the Mets in a 2-1 hole in the fourth. Hilliard, who entered batting .195 with two homers, also had homered in the second inning.

Jeff McNeil’s second homer of the series and 22nd of the season gave the Mets a 1-0 lead in the first inning. The Mets loaded the bases against Hoffman with two outs in the third, but Robinson Cano was retired to kill the rally.

Purchase event tickets to The Amazin’ 1969 New York Mets: A World Championship for the Ages presented by The Paley Center for Media and New York Post. Enter promo code: NYPOST to unlock tickets only available for Post readers.