The bullpen had saved the game. Then, it had lost it.

Finally, Mike Baxter won it.

And for the third straight game, the Mets walked off in dramatic fashion.

After eight innings of roller-coaster baseball, the Mets pulled out a 3-2 victory over the Pittsburgh Pirates Thursday night at Citi Field, courtesy of Baxter, who smacked a hard-hit single up the middle to score Marlon Byrd in the ninth inning with the winning run.

Baxter came up to pinch-hit in the ninth and to replace center fielder Juan Lagares — who saved the game in his own right in the eighth by robbing Pirates star Andrew McCutchen of a potential home run.

Which was why even in a performance devoid of a signature moment, Mets starter Dillon Gee would like to bottle what he did in five-plus innings Thursday night. And his manager would like to do the same with the four innings of relief that came behind him.

Gee pitched three batters into the sixth inning — scattering five hits and allowing just one earned run. It wasn’t the most efficient performance — he struck out only three and walked two in 101 pitches — but it kept his team in the game. And when Gee finally got himself into trouble by loading the bases to start the sixth, it looked like this night would go the way of so many other this season.

But the bullpen held.

First it was Brandon Lyon, who surrendered a sacrifice fly to third baseman Pedro Alvarez, which allowed center fielder Andrew McCutchen to score. But Lyon proceeded to sit down the next two batters in the Pirates’ lineup — second baseman Brandon Inge and shortstop Clint Barnes — to get out of the jam.

Mets reliever Scott Rice relieved Lyons in the seventh after a runner got on and worked his way out of it.