The two main British political parties suffered a blow at a byelection on the island of Anglesey in north Wales on Thursday after Ukip pushed the Tories into fourth place and only narrowly lost out to Labour.

Welsh nationalists Plaid Cymru easily won the seat in the Welsh assembly, leaving Labour, which governs in Wales, trailing way behind in second place. Ukip comfortably beat the Tories and finished only 336 votes behind Labour.

The result is galling for Labour, which would have taken overall control of the assembly if it had won the Ynys Môn seat. It still holds 30 of the 60 seats in the Cardiff-based assembly.

That Ukip has done so well away from its English heartlands will disturb the Conservatives. It was also a poor night for the Liberal Democrats, who finished last with 309 votes.

The byelection was called after the former Plaid leader Ieuan Wyn Jones announced he was standing down to take on a new role outside politics.

Though Plaid held the seat, the result was by no means a given. Ynys Môn has a colourful political history, having been represented by MPs from four different parties in the past 60 years.

Ap Iorwerth said: "I am delighted that the people of Anglesey have put their faith in me to represent them. I am passionate about building a brighter future for the people of the island, where I was raised and where I am raising my family."

Ukip's Nathan Gill said he was disappointed not to have beaten Labour but that the result showed his party was "not just a protest vote party" any more.

"We've come a long way and it has shown that our message is continuing to resonate with ordinary people up and down the country," he said.