But the biggest gainers on the night were the Lib Dems who saw a 19.3% swing from the Conservatives in Witney - a seat where they finished a distant fourth last year - overtaking Labour which fell back to third.

A jubilant Lib Dem leader Tim Farron said the result was a rejection of the Tories' plans for a "hard Brexit" while marking a return to the "political big time" for his party after their disastrous general election performance.

"The result not only signals that the Liberal Democrats are back in the political big time and the return to three-party politics, it is a clear rejection of the Conservative Brexit government's plan to take Britain out of the single market. This was the 10th safest Tory seat in the country with a massive 25,000 majority, yet the Conservatives were seriously rattled," he said.

"They are riding high in the polls, but my sense is that has largely been because people did not feel there was a real opposition to the Conservative Brexit government. Witney proves there is now a real opposition, and that opposition is the Liberal Democrats."

In his acceptance speech, Courts paid tribute to Cameron - whose decision to quit Parliament triggered the contest - as "a great prime minister and a brilliant MP" for the Oxfordshire constituency.

"It did not matter who you were, where you lived or how you voted, if you had a local issue then he was always happy to help and represent local people - that is something I will strive to do and to continue in my time as Member of Parliament," he said.

"Now we are going to move forward to build a country that works for everyone, our society should work for everyone, our economy should work for everyone and our democracy should work for everyone."

The Conservatives took 17,313 votes - just over 45% of the vote share - down from Cameron's 60% in 2015 - while the Lib Dems with 11,611 received 30% of the vote, up from 6.8% last year.

(Chris Radburn/PA)

Labour, with 5,765 votes, gained just under 15% - down slightly from the 17% it had at the general election. All the other candidates lost their deposits.

There was better news for Labour in Batley and Spen where former Coronation Street actress Tracy Brabin took 86% of the vote, with the nine independent and fringe party candidates who stood against her all losing their deposits.

In her acceptance speech, Brabin - who faced a challenge from hard right candidates from both the National Front and the BNP - said her win was a victory for "hope and unity".

(Danny Lawson/PA)

"This has been a difficult experience for all of us and tonight is a bittersweet occasion for me. That this by-election has had to take place at all is a tragedy," she said, amid noisy heckling from supporters from some of the rival candidates.

"I hope Jo will be proud tonight of our community. We have shown that we stand together with one voice choosing unity and hope."