Defending the decision to call the referendum, which he believed he could win, Cameron said: "We should be proud of the fact that we trust the people with these decisions."
He congratulated the Leave campaign on the "spirited and passionate case they made" and added:
"The will of the people is an instruction that must be delivered."
Cameron said it was right for him to stay on to deal with the immediate fallout of the result, but that another leader should be in place to handle the negotiations with the EU. This means that triggering article 50 of the Lisbon treaty, which will get the ball rolling on separation, will take place under a new Prime Minister.
"I will do everything I can as Prime Minister to steady the ship over the coming weeks and months, but I do not think it would be right for me to be the captain that steers our ship to its next destination," Cameron said.
Tory MPs who supported Leave, including Boris Johnson and Michael Gove, had signed a letter calling for Cameron to stay on - but it was always thought likely he would choose to step down anyway.
Sterling dived to a 30 year low against the dollar of $1.32 earlier after the result.
Shares in advertising and media groups including ITV and WPP tumbled amid stock market turmoil. ITV shares crashed as much as 20% or nearly 50p at one point to 170p as City traders feared a short-term hit to ad revenues.
WPP fell more than 4% or 70p to £15.20. The world’s biggest ad group generates about one eighth or around 12% of its global revenues from the UK.
And rating agency S&P confirmed that the UK was likely to lose its final AAA credit rating, with chief ratings officer Moritz Kraemer telling the Financial Times he believed the UK’s top rating was "untenable under the circumstances". The UK government will be informed 24 hours before any decision is announced.
WPP CEO Sir Martin Sorell, one of the industry's leading campaigners for a Remain vote, said he was "very disappointed, but the electorate has spoken."
Sorrell added: "The resulting uncertainty, which will be considerable, will obviously slow decision-making and deter activity. This is not good news, to say the least. However, we must deploy that stiff upper lip and make the best of it."
Chris Hirst, European and UK group CEO at Havas, said it was important to put the result in perspective.
"I didn't support leaving the EU, but despite the surprise of this result, nor do I agree with those who predict disaster," he said.
"The UK will continue to lead the world in the depth and breadth of it's creative talent and creative industries. The fundamentals stay the same and while there will be much to discuss and plan for, it is important we don't lose sight of our talent, strength and global influence."
Despite indications that Remain would win – including comments from Nigel Farage last night that he believed that to be the case – Leave took 52% of the vote, including a clear majority in Wales and all regions of England except London.
Remain, however, was far ahead in London, Northern Ireland and Scotland, immediately reigniting questions about the future of the UK, which many have speculated could be at risk from this result.
263 of the UK's 382 council areas voted Leave, while 118 voted Remain, including every area of Scotland and almost all in London. Manchester, Liverpool, Newcastle, Leeds, Bristol and Brighton & Hove voted Remain, while Brimingham and Sheffield narrowly voted Leave.