The dramatic announcement came after a sharp fall in the pound and as £128bn was wiped off the FTSE 100.
Cameron said: “I am honoured to have been prime minister of this country for six years.”
Earlier on Friday, Nigel Farage, the Ukip leader, became the first party leader to call for Cameron to go, saying what Britain needed was a “Brexit prime minister”.
Cameron’s team in Downing Street were shocked and distraught by the narrow win for leave, after polls had suggested a move towards a comfortable margin for remain in the final few days of campaigning.
The prime minister and the chancellor, George Osborne, had gambled their political futures on the historic referendum, which was called to settle the deep divide within their own party.
But a narrow victory for remain early in the night for Newcastle, which had been expected to reject Brexit by a strong margin, set the pattern for later results, which saw voters rejecting the overwhelming advice of economic experts that leaving would be an act of “economic self-harm”.
In the Labour stronghold of Sunderland, leave led with more than 61% of the vote. Nuneaton, famously the UK’s bellwether, went 66% for leave.
The value of sterling slumped to a 31-year low on currency markets, and was on course for its biggest one-day loss in history, as it became increasingly clear that voters had rejected the overwhelming consensus at Westminster, and chosen to exit the European Union.
Treasury officials were preparing to implement contingency plans for calming financial markets that were drawn up in the run-up to the poll, and the governor of the Bank of England, Mark Carney, was also expected to make a statement.
Both the BBC and ITV called the result for leave at around 4.40am. Farage had declared victory 20 minutes earlier, having earlier conceded he had probably lost, saying the British people had achieved a revolution, “without a single bullet being fired”.
Farage also criticised the pledge, at the centre of the cross-party Vote Leave campaign’s appeal to the public, to claim that leaving the EU would free up £350m a week to be spent on public services.
“This, if the predictions now are right, this will be a victory for real people, a victory for ordinary people, a victory for decent people,” he said.
Farage had been criticised for what some saw as the divisive tone of Leave.EU’s campaign, including a controversial poster picturing a queue of migrants, with the slogan, “Breaking Point”.
The prime minister had planned to announce a series of policy initiatives immediately, to reunify his fractured party and relaunch the government.
Instead his team in Downing Street were contemplating the end of their political project.
James McGrory, the Stronger In campaign’s chief spokesman, told the Guardian: “From the campaign’s point of view, the British voter has spoken, and we respect their decision, but it is now over to the government. Our role here is done.”
Gisela Stuart, the Labour MP who had backed the cross-party Vote Leave campaign, gave part of her victory speech in German, saying Britain would remain an open and friendly country.
“People were given the impression they had no choice but to remain, but they voted to Leave. It is incumbent on all of us to be very calm and remember our responsibility for the future of the United Kingdom,” she said.
The shadow chancellor, John McDonnell, said: “People will be waking up this morning to turmoil in the markets and the pound crashing, and fearing the emergency budget the chancellor threatened to hike their taxes and cut public services.
Hilary Benn, the shadow foreign secretary, had earlier said Cameron would have to resign if the UK voted for Brexit. “If there were to be a vote to leave, then as far as the prime minister is concerned I don’t see how he is going to remain in his job for very long at all,” he said.
Countries wishing to leave the EU must trigger article 50 of the Lisbon treaty, which kicks off a two-year process of negotiations with the other 27 member states.
The Labour leader, Jeremy Corbyn, had earlier called for the prime minster to trigger article 50 – the formal process for a country withdrawing from the European Union.
“The whole point of the referendum was that the public would be asked their opinion, they’ve given that opinion and it’s up to parliament to act on that opinion.”
This article was written by Heather Stewart and Jessica Elgot, for theguardian.com on Friday 24th June 2016 08.23 Europe/Londonguardian.co.uk © Guardian News and Media Limited 2010