Speaking outside 10 Downing Street this morning, he said he would attempt to “steady the ship” over the coming weeks and months but that “fresh leadership” was needed.
Cameron had urged Britons to vote Remain but was defeated by 52% to 48% despite London, Scotland and Northern Ireland backing staying in.
UKIP leader Nigel Farage hailed it as the UK’s “independence day”.
The public face of the Vote Leave campaign, Boris Johnson – a former London mayor and now frontrunner to be next prime minister – said voters had “searched in their hearts”, and the UK now had a “glorious opportunity” to pass its own laws, set its own taxes and control its own borders.
The pound fell to its lowest level against the dollar since 1985 as the markets reacted to the results.
Cameron stated that he had informed the Queen of his decision to remain in place for the short term, and to then hand over to a new prime minister by the time of the Conservative conference in October.
It would be for the new prime minister to carry out negotiations with the EU, and invoke Article 50 of the Lisbon Treaty, which would give the UK two years to negotiate its withdrawal, he said.
You can read Cameron’s full resignation speech here.
The referendum turnout was 71.8% – with more than 30 million people voting – the highest turnout at a UK-wide vote since 1992.
Mr Farage – who has campaigned for the past 20 years for Britain to leave the EU – told cheering supporters “this will be a victory for ordinary people, for decent people”.
Pro-Leave Conservatives including Boris Johnson and Michael Gove – who are both likely to be among contenders to be the party’s next leader – had signed a letter to Mr Cameron overnight urging him to stay on whatever the result.
Mr Johnson made no comment as he left his London home where a large crowd hurled verbal abuse at him.
Labour leader Jeremy Corbyn, who called for the UK to remain in the EU but was accused of a weak campaign, said poorer communities were “fed up” with cuts and felt “marginalised by successive governments”.
He said the point he had made during the campaign was that “there were good things” about the EU but also “other things that had not been addressed properly”.
Lib Dem leader Tim Farron said Labour’s leader had been “utterly gutless” in the way he approached the campaign.
Former Labour Europe Minister Keith Vaz described the outcome as “catastrophic for our country, for the rest of Europe and for the rest of the world”.
The European Parliament is to hold an emergency session on Tuesday to discuss the referendum result.
On Twitter, EU Parliament president Martin Schulz called for a “speedy and clear exit negotiation”.
Germany’s foreign minister Frank Walter Steinmeier described it as “a sad day for Europe and Great Britain”.
France’s Front National leader, Marine Le Pen hailed the UK vote, “Victory for freedom. As I’ve been saying for years, we must now have the same referendum in France and other EU countries.”
Dutch anti-immigration politician Geert Wilders said the Netherlands deserves a “Nexit” vote.
The leader of Italy’s anti-immigration Northern League party, Mateo Salvini, said: “Hurrah for the courage of free citizens! Heart, brain and pride defeated lies, threats and blackmail. Now it’s our turn.”
The anti-immigration Sweden Democrats wrote on Twitter that, “Now we wait for Swexit!”