At the end of a game pockmarked by bunts — bunts for hits, bunts sent foul, bunts popped up — it was a good old-fashioned long ball that won it for the Mets.

Todd Frazier’s go-ahead two-run homer in the bottom of the eighth lifted the Mets to a 7-3 win against the Giants on Thursday. With one out and a runner on second, Frazier reached for Mark Melancon’s curveball down and on the outer half of the plate, but hit it hard enough for it to carry over the wall in leftfield.

“I actually squared it up, as silly as it sounds,” Frazier said. “It wasn’t even a good pitch to hit. For me to find the barrel and get it on there, I couldn’t be happier. … More [often] I’m going to foul it off or miss it wildly. Just one of those times where I found a way to get a good piece on it.

Said manager Mickey Callaway: “We were like, ‘Get in the gap, get in the gap.’ Then it goes over the fence. … He won us the game on an excellent pitch by a really good pitcher.”

The Mets followed with what Callaway was yearned for them to do in recent weeks: Continuing to score after taking a lead. Too often lately, as the Mets (30-32) have treaded water against mostly mediocre-or-worse teams, they have jumped ahead early only to allow the other team to keep it close.

Not this time. Juan Lagares (2-for-3) and Jeff McNeil (2-for-2 off the bench) added RBI knocks for insurance runs in the eighth. On the former, Adeiny Hechavarria blew through third-base coach Gary DiSarcina’s stop sign to score easily on Lagares’ double into the leftfield corner.

The Mets will need more of that during a critical month ahead. From Friday through the end of the first half (July 7), they will play only teams that are at or near the top of their divisions. Of those 28 games, 15 are at Citi Field, where the Mets are 17-10 this year.

“I love the way we play in this ballpark,” Callaway said. “It’s kind of broken up by a big series across the way over there (visiting the Yankees Monday/Tuesday), and we’re excited about that. So we have to beat the best to be the best, and we’re going to go out there and do that.”

On Thursday, the Mets tied the game in the seventh with a series of improbable events. Lagares (career walk rate under 5 percent) drew a walk. Tomas Nido twice failed to get a sacrifice bunt down, sending them foul, but ended up with a single on a groundball to shortstop. Pinch hitter Carlos Gomez did manage to sacrifice bunt the runners over, but popped it up and initially didn’t run to first, ruining his chance of reaching base.

“He got the bunt down,” Callaway said, pausing to realize that “down” wasn’t quite the right word. “Scarily.”

Pinch hitting for Amed Rosario, McNeil did what McNeil does, sending a two-strike soft bloop to left, just behind shortstop Brandon Crawford, for an RBI single.

The Mets reached Shaun Anderson, a rookie righthander, for three runs in six innings. Of the six hits he allowed, two were homers (Rosario and Smith back-to-back in the first) and two were bunt singles (Smith and Lagares).

Zack Wheeler allowed three runs in seven innings, a good outing that was close to being great. Brandon Belt and Pablo Sandoval homered. Wheeler allowed only three hits.

“I felt a lot better today than I have in the past,” Wheeler said. “The results didn’t show up, but it was those two hits that really got me.”