The move, backed by the party hierarchy, was carried by a majority of just 29 votes after a passionate debate at the party conference in Perth.
In a tense vote, delegates voted 394 to 365 to reject an amendment by rebel MSPs and party members that would have reaffirmed the party's anti-Nato stance, against the wishes of First Minister Alex Salmond and defence spokesman Angus Robertson.
A further vote to remit the Nato U-turn back to SNP policymakers for further consideration was also narrowly voted down by 425 to 360.
Mr Robertson's new pro-Nato policy was finally approved by 426 votes to 332 after half an hour of counting votes by hand.
The U-turn means that the SNP will now apply to keep Scotland in Nato if it is elected to lead the first post-independence government.
Party rebels led by MSP Jamie Hepburn and seven other MSPs tried in vain to quash the U-turn and reaffirm the party's support for non-Nato alliance Partnership for Peace.
The SNP had opposed Nato for decades, previously arguing that it is a nuclear alliance at odds with the party's anti-nuclear stance, but that all changed today.
Mr Robertson insists that Scotland will only remain in Nato on the condition that it will be allowed to remove nuclear weapons from the Clyde.
The rebels fear that Scotland will face pressure to retain them if it remains in Nato.
Some defence experts and officials from the Ministry of Defence and the Foreign Office have warned that Nato membership cannot be taken for granted, suggesting that the Nato top brass may take exception to the SNP's anti-nuclear stance.
Earlier today, a former Nato secretary-general today warned that an independent Scotland's membership of the organisation is "uncertain at best". Lord Robertson of Port Ellen questioned whether membership of Nato would even be open to Scotland under the terms of the SNP's new policy.
During the debate, Angus Robertson laid out his widely-trailed arguments for the new pro-Nato policy, chiefly the concerns of Scotland's neighbours that withdrawal from Nato would "pull the plug" on the radar that protects the North Sea.
"Given the information we now have from our neighbours we must fulfil our treaty obligations including mutual defence guarantees and conventional cooperation," he said.
"We should make sure that we continue to have the best relationship with our neighbours. We must do so within Nato as a non-nuclear country just like Norway, Denmark and so many others."
Rebel MSP John Finnie described Mr Robertson's fears about a radar "black hole" over Scotland as "total nonsense", and suggested Nato membership would bring pressure to keep nuclear weapons and reject causes such as Palestinian statehood.
"Vote to join Nato and there will be pressure on this man (Alex Salmond) and his deputy not to be involved with the CND (Campaign for Nuclear Disarmament), not to support the Palestinians and similar causes around the globe," he said.
SNP trade unionist Bill Ramsay suggested SNP ministers are already under pressure from pro-Nato figures.
"We see today that many delegates are under pressure, parliamentarians under pressure, and even possibly some ministers under pressure," he said.
"We see Norway under pressure to buy kit it doesn't need. We see Germany under pressure to accept tactical nuclear weapons. The mighty Germany has tactical nuclear weapons like poo on the shoe that it is trying to remove, but finding that not only can they not remove it, but it's being upgraded."
He received loud applause and cheers when he called on delegates to "consider Nato membership when the last Trident boat sails down the Clyde, and not before".
MSP Jamie Hepburn, who has led the rebel cause, said Germany has found it "nigh-on impossible" to remove nuclear weapons under pressure from Nato.
"Why should we expect it to be simple for Scotland to remove them?" he asked.
He added: "Even if it were possible to remove nuclear weapons from Scotland and remain a member of Nato, where is the morality in seeking to rid our own country of the abhorrence of nuclear weapons but sheltering under the umbrella of an organisation that retains a nuclear first strike policy?"
Mr Hepburn added that the world may be "sleepwalking back into a new cold war".
"In this post-Cold War world we must question what Nato is a force for in the 21st century," he said.
"Does the continued existence of Nato contribute to positive relations with other states around the world such as the Islamic Middle East or Russia, who rightly or wrongly regard Nato as inherently hostile to their interests? Do we really want to be part of such an entity?"
MSP Jean Urquhart railed against being called "a rebel" for supporting the amendment that would have reaffirmed the party's then existent anti-Nato stance.
Fellow MSP Rob Gibson said there "is no proof that Nato will allow Scotland to remove nuclear weapons...It is an assertion plain and simple," he said.
It is understood some party delegates now fear the Nato issue has opened up a split in the party at a time when they are trying to present a united front in the run-up to the independence referendum in 2014.
Glasgow councillor Norman MacLeod rejected talk of party division, but received applause from a large section of the room when he said Mr Robertson's pro-Nato stance is based on "unsubstantiated assertion".
"We will leave after we have voted today united and continuing in our passion to achieve independence," he said.
He added: "Do we want to be friends with people, do we want to do good in the world, do we want to have an appropriate defence policy sufficient to the needs of Scotland? Absolutely.
"But then, in my view, (the resolution) goes into unsubstantiated assertion and detail."