Media playback is unsupported on your device Media caption Boris Johnson pledged to "continue to fight for a good deal for Londoners"

Boris Johnson has won a second term as London mayor, beating Labour rival Ken Livingstone by 3%, after a far closer contest than expected.

Mr Johnson won on second preference votes after failing to gain more than 50% in the first round.

He bucked the national trend after heavy Tory losses elsewhere.

Lib Dem Brian Paddick saw his vote collapse and he was beaten into fourth place by Green Jenny Jones, with independent Siobhan Benita fifth.

Mr Johnson's victory comes after a dismal night at the polls for Conservatives across England, Scotland and Wales, as Labour seized control of 32 councils.

Labour also saw a significant boost in their vote across London in the Assembly elections - but many of the party's voters appear to have shunned Ken Livingstone when it came to choosing a mayor.

In other developments:

Labour gained 823 councillors nationally, as the Tories lost 405 and the Lib Dems 336

Labour had a projected 38% national share of the vote, up three points, with the Tories down four on 31% and the Lib Dems unchanged on 16%

The Lib Dems were the big losers in Scotland , where Labour and the Scottish National Party made big gains

Voters in Birmingham, Sheffield, Newcastle, Wakefield, Manchester, Nottingham, Bradford, Leeds and Coventry rejected the mayoral system - but Bristol voted in favour and Doncaster voted to keep theirs

Labour made substantial gains in Wales, including taking Cardiff

Mr Johnson gained 44% of first preference votes, to Ken Livingstone's 40.3%. After second preferences came into play, Mr Johnson gained a total of 1,054,811 votes, or 51.5%, to the Labour candidate's 48.5% - making it an even closer contest than in 2008.

Media playback is unsupported on your device Media caption "This is my last election"

He outperformed the Conservative Party as a whole across the country, and was asked by the BBC's Evan Davis, after his victory, why that might be.

"I think the mayoral election is genuinely an election between two candidates who are, as you can see, for one reason or another, thought to be distinct from their parties in some senses - you saw that with both Livingstone and to an extent me," he said.

"But my programme is absolutely, avowedly conservative with a big C or a small c."

There has been speculation that Mr Johnson could be a future Conservative Party leader, but he told the BBC he was dedicating himself to London and people could "take it for granted" that he would not stand as an MP at the 2015 general election.

Conservative Home Secretary Theresa May said Mr Johnson's victory was "a great result", and admitted it had been "a difficult set of elections for us this time round".

"We recognise that, but it was difficult times and it was against a very hard backdrop," she said.

Mr Johnson failed to secure the massive win predicted by some opinion polls - and at one stage, as the count was delayed, it looked as if the Labour candidate might overtake him.

'Negativity'

Mr Livingstone announced his apparent retirement from frontline politics in his losing speech, saying "this will be my last election".

Later, he apologised for failing to secure victory, but said the campaign had been "vicious and unpleasant" and blamed "incredibly slanted" media coverage which he said had led to his key pledge - to cut fares - being "marginalised".

He said that as mayor, Mr Johnson had "carried on opening the things I started", but not done anything to plan ahead for London's future.

"If we have another four years like that, then by the end of this decade London will be insufferable. The population's growing, you've got to massively expand housing, you've go to expand the transport capacity.

"It will be the mayor two down the road from here who then faces a real crisis."

Labour's London-wide Assembly vote was up 14.3% on 2008, while the Conservatives were down 4.7%.

Labour MP for Hackney North and Stoke Newington Diane Abbott - told BBC Breakfast there had been a "relentless media campaign" against Mr Livingstone but that people across the country were turning away from the coalition government.

"Whereas these local elections have proved to the Labour Party that we're going in the right direction - even if we've still got a lot to do - they're proving to Cameron that he's in the wrong direction," she said.

The BBC's political correspondent Tim Reid said there was now huge pressure on both David Cameron and Nick Clegg to change course and drop certain policies from next week's Queen's Speech.

Some backbench Conservatives are urging Mr Cameron to abandon more liberal-minded policies like gay marriage, while some Lib Dems want Mr Clegg to put greater distance between them and the Tories.

UKIP blunder

Labour secured eight of the London Assembly's 14 first-past-the-post constituencies, gaining two from the Tories, which left them with six.

Media playback is unsupported on your device Media caption Boris Johnson: I was offering, in tough times, a cost-effective sensible approach

The 38.1% turnout in London was down 6.7% on 2008 when Mr Johnson and Mr Livingstone last went head-to-head in the race for City Hall.

To cap a dreadful night at the polls for the Liberal Democrats, mayoral candidate Brian Paddick lost more than half of his 2008 vote.

Independent Siobhan Benita, a political newcomer, was the surprise package of the night, threatening at one point to come third ahead of Mr Paddick and the Green's Jenny Jones.

She has told the BBC she plans to stand again in 2016, adding that people were "disillusioned with party politics" and wanted "a new type of public leader".

UKIP's hopes of returning to the London Assembly appear to have been dashed - but the party blamed an administrative blunder by their officials, which led to candidates being listed as "Fresh choice for London" rather than UKIP.

· All the latest election results are available at bbc.co.uk/vote2012