LEWISTON — The Portland Pirates got a little shaky at the start of the third period but avoided a complete collapse and snapped a four-game losing streak Thursday night.

Brendan Shinnimin and Tobias Rieder each scored two goals and Mark Visentin made 35 saves to lead the Pirates to a 6-4 win against the Worcester Sharks before 1,731 at the Androscoggin Bank Colisee.

The Pirates, who blew two two-goal leads and one three-goal lead in their last three games, gave up most of a three-goal lead less than five minutes into the third period before buckling down and hanging on.

“It wasn’t the way you’d write it up, but at this point to find a way to get the win was important just from a confidence standpoint,” Portland Coach Ray Edwards said. “Obviously the first shift of the third period wasn’t good enough, but we found a way to stop the bleeding, which was nice.”

“For a second there, it looked like it was going to be another one of those nights, but we stayed positive on the bench and we stuck together,” Shinnimin said. “We knew we were not going to let this one slip away. We chipped away and chipped away and found a way to get those two points.”

With the win, the Pirates (20-25-1-7) climbed out of last place and moved into a tie with the Syracuse Crunch for 12th place in the AHL’s Eastern Conference.

For the fifth consecutive game, the Pirates scored first. Brett Hextall scored on their first shot of the game less than six minutes in. Hextall’s ninth goal of the season came 22 seconds after he helped complete the Pirates’ first penalty kill.

Two minutes later, Travis Oleksuk made it 1-1 with his 11th goal of the season.

The Pirates regained the lead less than 12 minutes into the game when Shinnimin put in a rebound from the slot after Troy Grosenick stopped a shot by Brandon Yip.

The rest of the first period belonged to Visentin and the Pirates’ penalty killers, as the Sharks were on the power play for nearly six consecutive minutes, including a 1-minute, 36-second segment when the Sharks skated with a two-man advantage. Visentin made 10 of his 19 first-period saves while the Pirates were short-handed.

The Pirates stretched their lead to 5-2 in the second period.

Rieder made it 3-1 when he scored on their second power play of the game, 11 minutes into the period.

With less than five minutes left in the period, Konrad Abeltshauser of the Sharks scored a short-handed goal to cut the Pirates’ lead to one, but Andy Miele scored his team-high 22nd goal and Shinnimin scored his eighth goal over the next two minutes to send Portland to the locker room with a three-goal lead.

Worcester got goals from James Livingston and Rob Davison in the first five minutes of the third to cut the lead to one.

“The first half of (the third) period we were scrambling, but we calmed down and began to communicate a lot better,” Shinnimin said. “Once we did that, we started getting pucks back in their own end.”

With 29 seconds left, Rieder scored his 20th goal of the season into an empty net.