The Conservatives have won the Newark by-election, retaining the seat with a majority of more than 7,000.
Candidate Robert Jenrick polled 17,431 votes, beating UKIP's Roger Helmer, who finished second with 10,028 votes.
Labour's Michael Payne came third with 6,842 votes but it was a disastrous result for the Lib Dems, who were beaten into sixth place.
Chancellor George Osborne said the victory was a "strong endorsement" of the Conservatives' economic plan.
The resignation of Newark's former Conservative MP Patrick Mercer, who quit over a cash-for-questions scandal, had raised expectations in UKIP - on a high from their recent European election victory - that they could cause a major upset by winning their first seat at Westminster.
But the Conservatives flooded the seat with MPs and activists in the run-up to polling day - with David Cameron visiting the Nottinghamshire market town four times - in their bid to retain what was, in theory, a safe seat.
In the end, Mr Cameron's party won a relatively comfortable victory, although UKIP succeeded in more than halving the party's 16,000 majority that it gained at the 2010 General Election and increasing their share of the vote by 22.1%.
In finishing third, Labour polled 17.68% of the vote, down 4.65% from the general election.
BBC Political correspondent Alex Forsyth writes: After UKIP's success in the European elections, some feared the political earthquake the party had promised. In Newark it was more of a tremor, but without doubt one that shook the ground under the feet of some of their political opponents.
The party failed to get its first MP elected to Westminster, but it did leave a dent in the Conservative majority, although not as much as Mr Farage had hoped - or predicted. David Cameron will use this by-election victory to say UKIP's bubble has burst and he has stopped the "people's army" in its tracks - but it took an army of his own to do it. The Conservatives inundated Newark during the short campaign.
David Cameron said it was a "very good" result for for the Conservatives but acknowledged the Tories needed to "work between now and the next election" to win back voters from UKIP.
Mr Osborne said the Conservatives had got more votes than UKIP and Labour combined, saying the opposition's performance had been "disastrous".
"We have won this seat with a big majority," he told BBC Radio 4's Today programme. "This is the first time in 25 years that the Conservative have held a by-election in government.
"We can take some comfort from this result but we know, of course, that the job is not done. We have to get out there and take our message to the other constituencies over the coming year."
In his victory speech, Mr Jenrick, a 32-year-old managing director at Christies, said: "I want to thank the prime minister for his personal support to my campaign and I want to thank the government for its commitment to re-building Britain.
"I hope now that I can repay the faith and trust that the people of Newark have put in me as your new member of parliament - and in the months and years to come I can build a reputation as a strong and effective MP."
UKIP leader Nigel Farage - who turned down the chance to stand as his party's candidate in Newark - conceded defeat before the result was announced but said his party had retained 85% of the support it won in European elections.
"I think this has been a stunning campaign that we've fought in a very short space of time.
"We've been up against probably the biggest ever Conservative machine, defending about their 40th safest seat in the country. If the indications are right, we'll be celebrating a massive advance for our party."
Labour MP Chris Bryant, who ran his party's campaign, said the Tories' win was no surprise: "This is the 44th safest Conservative seat in the country.
"There are cabinet ministers who wouldn't be MPs if they were to lose seats like this.
"They didn't just throw the kitchen sink at it - they threw the butlers' sink, they threw the crockery, all the silverware, the Aga, the butler, the home help, everything at it.
"I think this really shows that the Conservatives - who haven't won a general election since 1992 - still haven't got a winning streak with them."
The Lib Dems' 1,004 votes, and 2.6% share of the vote, represents one of their worst performances in a post-war English by-election.
Lib Dem candidate Mr Watts said: "Well it wasn't a good result, but smaller parties often get squeezed in by-elections and that's what's happened to us here.
"We knew, from talking to people today, that a lot of our voters had transferred to vote against UKIP to make sure UKIP didn't get elected and some have clearly gone to Paul's [Independent candidate Paul Baggaley] campaign on the hospital which is a very important campaign."
Newark by-election: result in full
Robert Jenrick (Con) 17,431 (45.03%, -8.82%)
Roger Helmer (UKIP) 10,028 (25.91%, +22.09%)
Michael Payne (Lab) 6,842 (17.68%, -4.65%)
Paul Baggaley (Ind) 1,891 (4.89%)
David Kirwan (Green) 1,057 (2.73%)
David Watts (LD) 1,004 (2.59%, -17.41%)
Nick The Flying Brick (Loony) 168 (0.43%)
Andy Hayes (Ind) 117 (0.30%)
David Bishop (BP Elvis) 87 (0.22%)
Dick Rodgers (Stop Banks) 64 (0.17%)
Lee Woods (Pat Soc) 18 (0.05%)
Con majority 7,403 (19.13%)
15.46% swing Con to UKIP
Electorate 73,486; Turnout 38,707 (52.67%, -18.69%)