Image copyright AFP Image caption The UK voted to leave the EU but Scotland voted to remain

Scotland has voted in favour of the UK staying in the EU by 62% to 38% - with all 32 council areas backing Remain.

But the UK as a whole has voted to Leave - raising the prospect of Scotland being taken out of the EU against its will.

First Minister Nicola Sturgeon said Scotland had delivered a "strong, unequivocal vote" to remain in the EU.

The result prompted David Cameron to announce he would step down as prime minister by October.

Speaking in Downing Street, he said he would "steady the ship" until then - but would hand over to a new leader in time for the Conservative Party conference.

Scottish Conservative leader Ruth Davidson said he had served the office of prime minister "honourably".

She added: "His decision to remain in post over the short term is the right one, and will help ensure greater stability in the days ahead.

"It is right that he has already spoken to Nicola Sturgeon to ensure the Scottish government is involved in the negotiations that will follow.

"Our two governments need to work together for the benefit of all people in Scotland, and they will. All of us need to take stock of this decision, and put the stability of our country first."

US presidential hopeful Donald Trump, who has arrived in Scotland to visit his Trump Turnberry golf resort, meanwhile said it was a "great thing" that the people of the UK had "taken back their country".

With all the votes declared, the Leave side won by a margin of 52% to 48%.

Scotland's First Minister Nicola Sturgeon told a media conference a second independence referendum was "highly likely" after the UK voted to leave the EU.

The SNP manifesto for May's Holyrood elections said the Scottish Parliament should have the right to hold another referendum if there was a "significant and material change" in the circumstances that prevailed in 2014, such as Scotland being taken out of the EU against its will.

At a news conference in Edinburgh, Ms Sturgeon said: "It is, therefore, a statement of the obvious that a second referendum must be on the table, and it is on the table."

Leave polled more than 1.2 million votes more than Remain, with the English shires and Wales voting strongly in favour of Brexit

David Cameron has announced his intention to stand down as prime minister by October

UKIP's Nigel Farage said: "Dare to dream that the dawn is breaking on an independent United Kingdom."

The pound has fallen to levels not seen since about 1985

Turnout in Scotland was 67% while in Wales it was 72% and in England 73%.

Turnout was 56% in Glasgow and 63% in Dundee

Scottish Conservative leader Ruth Davidson has insisted the Tories will 'stay together'

Independence referendum

Media playback is unsupported on your device Media caption People in Glasgow were asked if there should now be another independence referendum

Her predecessor as first minister, Alex Salmond, was quick to suggest there should now be a second Scottish independence referendum.

Mr Salmond told the BBC: "It means that Nicola Sturgeon has to go forward with the manifesto, which as you remember said the Scottish Parliament should have the right to call a second referendum on Scottish independence if there was a material and significant change in the circumstances, like Scotland being dragged out of the European Union against the will of the Scottish people.

"Now that has happened and I'm certain that Nicola will go forward on that manifesto commitment."

Scottish Labour leader Kezia Dugdale described the result as "distressing" but said the priority now was best summed up by the Scottish phrase "keep the heid".

She said: "First and foremost our priority must be to stabilise the economy - to reassure people about their jobs, about their pensions and about the opportunities that are yet to come.

"It's going to be a long drawn out process now."

Scottish Liberal Democrat leader Willie Rennie said he was "angry that we have lost our place in Europe".

He said: "It is bad for our country and the people who live here. It means cutting our ties with our biggest economic market despite the consequences for trade, business, jobs and incomes.

"It means we are turning our back on co-operating with our closest neighbours on security, peace, the environment and workers' rights."

'Huge contribution'

Scottish Green co-convenor Patrick Harvie said Scotland "must keep open every option for protecting ourselves from this threat" and said it was "devastating to see that the deceitful, manipulative and at times downright racist Brexit campaign has succeeded south of the border".

Tom Harris, of Scottish Vote Leave, played down the Remain majority in Scotland, and said more than a million Brexit supporters in Scotland had made a "huge contribution" to the overall result.

He told BBC Scotland's Good Morning Scotland programme: "It's not the case that Scotland voted one way and England voted another. The voice of more than a million Scots cannot be simply be dismissed."

Earlier, Mr Harris predicted there would not be a second independence referendum after the EU result.

The Scotland Stronger In Europe campaign said the scale of the Remain majority in Scotland was "exceptional".

Spokesman John Edward said: "We are proud that every single local authority area in Scotland voted Remain with an overall lead of 24 points over Leave - that clearly stands out as an exceptional result compared to the rest of the UK.

"Scotland even recorded a higher level of support to stay in the European Union than in the 1975 referendum.

"We are pleased to have won well in Scotland, but of course the only result that decides the issue is the UK-wide vote."

Image copyright Getty Images Image caption Nigel Farage said the dawn was breaking on an "independent United Kingdom"

Overall, 1,661,191 voters backed Remain in Scotland, with 1,018,322 voting for Leave.

Glasgow voted by 168,335 (67%) to 84,474 (33%) in favour of Remain, although turnout was relatively low.

Edinburgh backed Remain by 187,796 (74%) to 64,498 (26%), but results in England and Wales were much better for Leave.

Moray saw the closest result in Scotland, with Remain finishing just 122 votes ahead of Leave.

But there were big wins for Remain in heavily-populated areas such as Aberdeen, Dundee, North Lanarkshire, South Lanarkshire, Fife and West Lothian.

Analysis by Philip Sim, BBC Scotland political reporter

Image caption Joy for Remain campaigners at Scottish counts was tempered by dismay as results elsewhere in the UK went in favour of Leave

Another day, another constitutional crisis.

The final results from the EU referendum weren't even declared before there was discussion of what happens next in Scotland.

Scots have been to the polls five times in the span of just over two years. Many may now be wondering if they'll be back again soon.

The overbearing theme in the counting halls was one of tension.

Remain knew they had done well north of the border, but were startled by the strong showing Leave recorded in the early results across England and Wales.

In Edinburgh, for example, Remain scored a whopping win. But the cheers from the gathered few observers were rather muted, with the national result hanging in the balance.

The pattern was repeated across the country - albeit with smaller margins of victory in some places, such as Moray, where less than 200 votes separated the sides.

And in the end, when the Scotland-wide result was announced in Grangemouth, it was the Leave side who were slapping each other on the back and jumping for joy, despite losing the Scottish vote by 62% to 38%.

This result will raise a lot more questions than it answers in Scotland.

Orkney became the first Scottish area to declare its result at about 00:05, with Remain winning 7,189 votes (63%) and Leave on 4,193 (37%). Turnout was 68.4%.

It was followed by Clackmannanshire, which backed Remain by 58% to 42%. A majority of voters in Shetland, the Western Isles and the Scottish Borders also backed Remain.

Image copyright Getty Images Image caption Nicola Sturgeon, who voted in Glasgow on Thursday morning, said Scotland had delivered a "strong, unequivocal vote" to remain in the EU

YouGov had published an on-the-day poll which had Remain on 52% and Leave 48%, while an Ipsos-Mori poll from Wednesday and Thursday put Remain on 54% and Leave on 46% - but the pollsters got the result badly wrong, as they had done in last year's general election.

Image copyright PA

About 46.5 million people were registered to vote - including four million in Scotland - in what was the third nationwide referendum in UK history.

Find the result in your area