The Alaska Supreme Court on Wednesday reversed a lower court ruling that had awarded a contested election in Alaska's northernmost state House district to the incumbent, instead declaring challenger Dean Westlake the winner.

In a decision issued just hours after hearing arguments in its Anchorage courtroom, the justices said they were reinstating the original results of the Democratic primary election in August, giving Westlake an eight-vote margin over incumbent Ben Nageak.

Anchorage Superior Court Judge Andrew Guidi last week had overturned that result, throwing out 12 votes he said had been inappropriately cast in the Northwest Alaska village of Shungnak, which gave Nageak a two-vote win.

But the higher court disagreed, reversing Guidi's decision in a one-page order. One of the five justices, Daniel Winfree, wrote a partial dissent saying the court should have ordered the election rerun, rather than upholding the original results.

A written ruling "will be forthcoming," the order said; the justices were trying to issue a decision this week so the state Division of Elections could get absentee and other ballots to voters before legal deadlines.

Westlake, 56, faces no Republican challenger in next month's general election, so Wednesday's decision effectively awards him the state House seat. His candidacy this year was his second in a row; he lost a challenge to Nageak in 2014 by 130 votes, or 53 percent to 47 percent.

"Holy mackerel. I'm stunned at this quick turnaround," Westlake said by phone from the Northwest Alaska hub town of Kotzebue, where he lives. "I'm absolutely elated — and not only for me, but especially for those voters out there."

Nageak, who's from the North Slope community of Barrow, was again relegated to defeated incumbent status after Guidi less than a week ago handed him a third term. Nageak didn't respond to a phone message Wednesday.

The Supreme Court's ruling gives a small but significant boost to urban Democrats, who hope to win enough seats to form a bipartisan coalition to control the House when it reconvenes next year.

The chamber is currently led by a mostly Republican majority, which also includes four Bush Democrats. Nageak was one of them, and his legal challenge of the initial election results was aided by a prominent GOP attorney, Tim McKeever, and Randy Ruedrich, former chair of the Alaska Republican Party.

Westlake has said he will likely caucus with urban Democrats even if that leaves him in the minority.

The stakes of the case were underscored by the robust attendance at Wednesday's hearing, which came before an audience of more than 40 people that included legislators, a lobbyist, and Alaska Attorney General Jahna Lindemuth.

"It's a big case," Lindemuth said afterward.

While the justices didn't issue their decision from the bench, several asked questions suggesting they thought Guidi went too far in reversing the results of the primary, rather than upholding the original count or ordering a new election.

"It was legal error, wasn't it?" Winfree asked an attorney for Nageak, Stacey Stone, at one point, referring to Guidi's reversal of Westlake's victory.

Wednesday's decision came after Nageak filed two legal challenges, one in Superior Court and one in the Supreme Court, contesting the original election results. His arguments cited reports of irregularities at multiple polling places across the sprawling District 40, which were documented by voters and news media.

Guidi dismissed most of Nageak's complaints. But he agreed election workers made a significant error in Shungnak, which went 47-3 for Westlake.

The Shungnak workers issued each voter two ballots — one with Democratic primary candidates, and the other with Republican candidates — when voters were instead supposed to choose one.

Guidi said the mistake amounted to "malconduct," the legal standard that an election challenge must meet to be successful.

He revised the election results by estimating how many Shungnak voters would have picked a Republican ballot if they'd been forced to choose, thereby proportionally reducing the number of Democratic primary votes for each of the two candidates, he said.

Guidi's calculation was 12 "contaminated votes" from Shungnak should be thrown out — 11 for Westlake and one for Nageak — which was enough to overturn the election's outcome. The state then appealed that decision, and the Supreme Court reversed Guidi's handling of the Shungnak ballots.

The justices didn't explain why in their brief order, but they gave some hints during Wednesday's arguments.

Justice Craig Stowers, said during the proceedings Guidi's reversal of the results was contrary to precedent, while another suggested redoing the election would be a more appropriate step.

"It's an extreme remedy that allows all of these voters to actually cast a vote. Isn't it more extreme for a judge to essentially select a number of voters and discount them, or to substitute his idea for what the voters did?" Justice Susan Carney asked Stacey Stone, who argued the case for Nageak on Wednesday.

The elections division had printed a set of general election ballots with both Nageak and Westlake's names on them, in case the justices ordered a rerun of the election.

But that step won't be necessary.

"The primary task of the Division of Elections is to run fair, impartial, and efficient elections in which every qualified voter is permitted to cast a ballot and know that their vote will count," Josie Bahnke, elections director, said in a prepared statement. "The division is pleased that the Supreme Court agreed that the election result as certified by the division should stand."

Westlake said he expects to resign his job as an economic development official with NANA Regional Corp. He wants to work on shoring up his relationships with residents of the Slope region who voted overwhelmingly for Nageak, and plans to start doing so at the Alaska Federation of Natives annual convention next week in Fairbanks.