It turned from ugly to thrilling at Hadlock Field on Wednesday night.

Nate Freiman swatted a three-run walk-off homer over the left-field wall in the ninth inning to give the Portland Sea Dogs a 10-7 win over the New Hampshire Fisher Cats before a capacity crowd of 7,368.

No one expected that much excitement after Portland fell behind 5-0 while making three errors in the first two innings.

“The game didn’t start off the way we would have liked,” Freiman said.

The Sea Dogs rallied, scoring four runs in the seventh to jump ahead 7-6. New Hampshire tied it in the eighth.

In the bottom of the ninth, Tzu-Wei Lin led off with a single against Brady Dragmire (4-6). Mauricio Dubon sacrificed Lin to second and Aneury Tavarez singled to put runners on first and third. Freiman then smacked Dragmire’s 2-0 pitch for the win.

“It was a slider,” said Freiman, sounding surprised. “I was just trying to be ready early and see it.

“I was trying to get something up and stay out of a double play. I wasn’t thinking home run, just something elevated.”

Freiman also had an RBI double. His home run was his 10th of the season and his second walk-off hit of the year. He had a game-winning single in May shortly after signing with the Red Sox as a minor league free agent.

Portland reliever Ben Taylor (1-0) got the win, pitching the final 12/3 innings.

Sea Dogs starter Kevin McAvoy rallied from his early struggles, providing 62/3 innings. He allowed six runs (three earned) on eight hits, one walk and one hit batter. He struck out three.

“It could have been worse but after the second inning he settled down nicely,” Sea Dogs Manager Carlos Febles said. “Really pleased with the way he pitched.”

This was McAvoy’s sixth straight start allowing three earned runs or less.

“(McAvoy) was huge for us,” Freiman said. “He gave us a chance to win.”

The Sea Dogs were paced by Ryan Court’s two doubles and one RBI. Dubon (2 for 4) had a key RBI single in the seventh and Tavarez (2 for 5) hit a two-run single in the seventh that tied it.

“The offense showed some courage tonight,” Febles said.

Center fielder Cole Sturgeon (1 for 3, RBI) made two impressive over-the-shoulder catches in the left-center gap, and also charged a fly ball and then doubled off the runner at first base in the fifth inning.

NOTES: Top Red Sox prospect Yoan Moncada took the night off. Moncada is being handled carefully after spraining an ankle almost three weeks ago. He played in three of Portland’s previous four games. … Tavarez is now batting .332, the best in the Eastern League … The Sea Dogs (51-76) will be home against the Yankees-affiliated Trenton Thunder from Thursday through Sunday. The Thunder (81-49) are close to clinching a playoff spot. … Before Thursday’s game, the Sea Dogs expect to welcome their nine-millionth fan. … The Fisher Cats (62-68) travel to Reading, Pennsylvania. … In advanced Class A, future Sea Dogs starter Michael Kopech pitched one-hit shutout ball over seven innings, walking two and striking out 10. He has a 1.23 ERA for Salem.