The Liberal Democrats held off a late Ukip surge to win the Eastleigh by-election today, but David Cameron was dealt a serious blow as the Conservatives finished third.

Nick Clegg said his party was "on track" for the 2015 general election after local councillor Mike Thornton held on to the seat vacated by disgraced ex-cabinet minister Chris Huhne.

Mr Thornton won with 13,342 votes, a majority of 1771 over the UK Independence Party's Diane James, who said beating the Tories represented a "seismic shift" in politics.

Tory Maria Hutchings polled 10,559 votes as Ukip snatched huge chunks of the coalition parties' 2010 general election vote share, taking more than 27% of the total.

Influential Tory backbench MP David Davis had warned the shock would mean "crisis" for his party but Conservative Party chairman Grant Shapps insisted you "can't read much into by-elections" – seeking to explain the poor showing as a classic mid-term voter revolt.

But the failure not only to overcome a LibDem party engulfed in the Huhne scandal and claims of sexual harassment by a senior party figure but also to outperform Ukip will reignite smouldering backbench dissent.

The Hampshire seat is one of 20 LibDem-held constituencies Mr Shapps has said are crucial for the party to win in 2015 if the Prime Minister is to secure an outright majority.

By contrast the result, bolsters the authority of the Deputy Prime Minister, who made a string of visits to the town.

On Twitter, Mr Clegg wrote: "Congratulations @Mike4Eastleigh: welcome to the team! We're on track for 2015 now."

In his victory speech, Mr Thornton said it was "a great night for the Liberal Democrats nationally, an affirmation of our role working in the national interest within the coalition and a huge boost to our party's mission in government to build a stronger economy in a fairer society".

Ukip leader Nigel Farage said: "The issues we have surged on in Eastleigh are not going away – they are going to get bigger and bigger."

Labour's candidate, satirist and author John O'Farrell, polled 4088 votes securing only a 0.2% increase on the party's 2010 general election share.

Elsewhere, Labour Party candidate Kevin Docherty won the Coatbridge West by-election for North Lanarkshire council. Mr Docherty, who secured 2145 votes, beat the SNP candidate Patrick Rolink into second.