PITTSBURGH — Marcus Stroman went from struggling to rolling to fading in the course of a night’s work Saturday, departing his Mets debut with his team at least still in the game.

One good rally was eventually needed to exonerate Stroman, and Wilson Ramos delivered it with a go-ahead homer in the eighth inning (before he drove in three additional runs in the ninth), sending the Mets to a 7-5 win over the Pirates at PNC Park. The victory was the Mets’ eighth in nine games, ensuring a winning road trip.

Stroman, the Long Island native who arrived in a trade last weekend that sent pitching prospects Anthony Kay and Simeon Woods Richardson to Toronto, joined Jacob deGrom and Noah Syndergaard among Mets starting pitchers with an All-Star pedigree. But this night was largely a slog for Stroman — perhaps the combination of first-game jitters with a new team and a long layoff between starts.

“I thought I was OK,” Stroman said after pitching 4 ¹/₃ innings and allowing three earned runs on seven hits and two walks in his first appearance since July 24. “Going five [innings] is where I should be at least at the minimum, so I just thought I was all over the place, command was a little iffy, but my body felt great. Just make a few adjustments to my mechanics and I think I will be where I need to for my next one.”

Ramos came to the rescue with a two-run blast in the eighth against Kyle Crick that put the Mets (54-56) ahead 4-3. In the ninth, Ramos delivered a three-run double that provided a cushion for Edwin Diaz, who surrendered a two-run homer to Starling Marte.

“I know [Friday] night I hit a couple of ground balls, but today I was feeling great at the plate, driving the ball the other way,” said Ramos, who finished 4-for-5 with a career-high six RBIs. “That’s my approach for all my career, and today I was feeling good to drive the ball the other way.”

Jeff McNeil’s first career pinch-hit homer, a solo blast in the seventh, had trimmed the Pirates’ lead to 3-2. McNeil, who began the night on the bench with a day off, smashed the first pitch he saw from Michael Feliz for his 12th homer this season.

Seth Lugo, who on Saturday was named National League Reliever of the Month, pitched a scoreless eighth before Diaz survived the ninth.

Stroman was removed with two runners on base in the fifth, at 92 pitches, and watched as Luis Avilan walked Jose Osuna before plunking Kevin Newman to force in a run, giving the Pirates a 3-1 lead.

Stroman encountered trouble in a first inning in which he faced eight batters and threw 35 pitches. The Pirates began with four straight singles against Stroman to take a 1-0 lead before the right-hander finally recorded an out. But Jose Osuna’s walk with the bases loaded gave the Pirates another run.

Before the inning could completely unravel, Stroman sprang from the mound to barehand Newman’s squib and fire to the plate for the second out. Jacob Stallings was then retired, finishing a 40-minute first inning.

“I think [the bare-handed play] gave me a little momentum and confidence, too,” Stroman said. “I thought I was making some good pitches, and they were putting some good swings on balls. I feel with my sinker I’m a double play away at any point in any game, so if I execute my pitches, like I didn’t today, I think I will be able to go deeper.”

Stroman allowed two singles in the second, but escaped the inning, during which he began a streak of eight straight batters retired that lasted into the fifth.

Chris Archer had unloaded 33 pitches in facing seven batters in the top of the inning, allowing one run. Ramos delivered an RBI single after the Mets had loaded the bases with one out.

After watching his team’s seven-game winning streak end the previous night, manager Mickey Callaway appreciated his team’s resiliency.

“Tonight was elusive to us in the first half,” Callaway said. “It was tough to stop the bleeding, and I think we understand we have to do that. I don’t think we’ve ever given up, but we just have to get it done.”