Voter confusion at the Nantucket ballot box changed the result of Tuesday’s Democratic primary for Cape and Islands state representative, according to the town clerk who managed vote counting where write-in candidate Tim Madden secured 52 per cent of the vote.

Mr. Madden ultimately lost to Vineyard candidate Daniel Larkosh, the West Tisbury attorney who won the nomination for the district that spans the Vineyard, Nantucket and Falmouth.

But Catherine Stover, the Nantucket town clerk, said enough ballots were cast in Nantucket bearing Mr. Madden’s name for him to have taken the district in yesterday’s race, had it not been written in the wrong places.

“He would have had it,” said Ms. Stover. She estimated that hundreds of ballots with his name on them did not count as votes for the candidate.

“He had handed out cards with everything on them [of how to vote] but people would forget the cards,” she said.

She added that a separate race on the ballot, for representative in congress, also caused some confusion.

“He’d say he wanted to be their representative,” she said, “there were votes in plenty of the wrong places.”

Tuesday was a night full of election surprises. Mr. Larkosh swept Falmouth with 888 votes and failed to carry the Vineyard, but the Falmouth vote put him well over the top.

On Nantucket 2,454 voters turned out, a primary record on that island. Mr. Madden’s overwhelming support on Nantucket was not mirrored in the rest of the district. He secured just 14 votes on the Vineyard and 120 votes in Falmouth, where write-in votes were not officially counted until Wednesday afternoon. The Falmouth vote brought his total vote to 1,402, putting him in second place above Tim Lasker of Chilmark.

Roger Wey of Oak Bluffs finished a distant fourth with 819 votes. Meanwhile Catherine Bumpus, a candidate who appeared first on the printed ballot but had withdrawn from the race months ago, garnered 642 votes.

Mr. Larkosh said late Tuesday once the outcome was known, that Falmouth was always his key to victory.

“It came down to Falmouth,” Mr. Larkosh said from the Portuguese American Club in Oak Bluffs where he was gathered with his supporters. “It played out the way we planned.”

Mr. Larkosh failed to win any of the six towns on the Vineyard, which went to Tim Lasker. Mr. Lasker won 40 per cent of the Island vote.

However, Mr. Larkosh came in second in each town, effectively splitting the Vineyard vote along with Mr. Wey.

“I got my third,” said Mr. Larkosh.

There were five candidates. The final tallies were as follows: Dan Larkosh, 1,721 (871 on the Vineyard); Tim Lasker, 1,320 (1,184 on the Vineyard); Tim Madden, 1,402; Roger Wey, 819 (728 on the Vineyard); David Moriarty, 744 (126 on the Vineyard); Catherine Bumpus, 642 (55 on the Vineyard).

A complete story on the primary election will appear in the Friday Gazette.