It was Groundhog Day with a twist for the Mets: They actually got it right on the second try.

After playing 14 excruciating innings in a loss to the Phillies on Friday, the Mets got even on Saturday, after David Wright’s RBI single in the 14th scored the go-ahead run in a 5-4 victory at Citizens Bank Park.

Carlos Torres got three outs to end the 5-hour, 32-minute freak show, but it was Buddy Carlyle, who arrived from Triple-A Las Vegas before the game to give the Mets a fresh bullpen arm, who did the best relief work.

Carlyle (1-0) pitched three shutout innings before Wright delivered against Antonio Bastardo.

“Ideally, for the long run, you never want to play 14-inning games, much less back-to-back,” Wright said. “But if you’re out there, you want to win. It was also nice to pick one up for the bullpen, because they had been so good.”

According to the Elias Sports Bureau, it’s the first time in franchise history the Mets played consecutive five-hour games. And the last time the Mets played consecutive games of at least 14 innings was Sept. 7-8, 1979 against the Pirates. The first of those games went 14 innings and the other went 15.

“We won it, and don’t ask me how, but I’ll tell you what: They are tired,” manager Terry Collins said. “There are some tired bodies out there.”

Jeurys Familia entered with the Mets leading 4-3 in the ninth, and blew the save. Marlon Byrd stroked a leadoff double and scored the tying run on Domonic Brown’s ensuing single. Familia then worked a scoreless 10th before getting the ball to Carlyle, who had last appeared in the major leagues with the Yankees in 2011.

“Just getting here this time was a lot harder than the first time I got to the major leagues,” said Carlyle, who manages Type 1 diabetes. “Getting back here at [age] 36, it’s real special.”

But even after Carlyle’s scoreless stint, the Mets had to survive a scare in the 14th when Torres allowed consecutive singles to start the inning before getting three outs.

If the game extended beyond the 14th, Collins was prepared to use catcher Anthony Recker to pitch. Recker was also on standby a night earlier, when Jenrry Mejia lost in the 14th inning.

The go-ahead rally for the Mets on Saturday started with Ruben Tejada’s walk in the 14th. Juan Lagares, who didn’t start the game because of spasms in his right rib cage, then entered as a pinch-hitter and bunted Tejada to second. According to Collins, Lagares’ only choice was to bunt: He was in no condition to swing. After Bastardo got two outs, Wright delivered an RBI single.

Rookie Jacob deGrom pitched 6¹/₃ innings for the Mets and allowed three earned runs on six hits with two walks and 11 strikeouts.