Iran and six major world powers capped more than a decade of negotiations on a nuclear deal with an agreement that could transform the Middle East.
US President Barack Obama hailed a step towards a "more hopeful world" and Iran's President Hassan Rouhani said it proved that "constructive engagement works". But Israel pledged to do what it could to halt what it called a "historic surrender".
The agreement will now be debated in the US Congress, but Obama said he would veto any measure to block it.
"This deal offers an opportunity to move in a new direction," Obama said. "We should seize it."
Under the deal, sanctions imposed by the United States, European Union and United Nations will be lifted in return for Iran agreeing long-term curbs on a nuclear programme that the West has suspected was aimed at creating a nuclear bomb.
Iran will mothball for at least a decade the majority of its centrifuges used to enrich uranium and sharply reduce its low-enriched uranium stockpile.
The agreement is a political triumph for both Obama, who has long promised to reach out to historic enemies, and Rouhani, a pragmatist elected two years ago on a vow to reduce the isolation of his nation of 80 million people.
Both face scepticism from powerful hardliners at home in nations that referred to each other as "the Great Satan" and a member of the "Axis of Evil".
"Today is the end to acts of tyranny against our nation and the start of cooperation with the world," Rouhani said in a televised address. "This is a reciprocal deal. If they stick to it, we will. The Iranian nation has always observed its promises and treaties."
Delighted Iranians danced in the streets of Tehran, whole motorists sounded car horns and flashed victory signs in celebration after the announcement a deal they hope will end years of sanctions and isolation.
For Obama, the diplomacy with Iran, begun in secret more than two years ago, ranks alongside his normalisation of ties with Cuba as landmarks in a legacy of reconciliation with foes that tormented his predecessors for decades.
"History shows that America must lead not just with our might but with our principles," he said in a televised address. "Today's announcement marks one more chapter in our pursuit of a safer, more helpful and more hopeful world."
Republicans lined up to denounce the deal. Presidential candidate Lindsey Graham, a senator from South Carolina, called it a terrible deal that would make matters worse. Senator Marco Rubio suggested he would re-introduce sanctions if elected to the White House next year.
The Republican-controlled Congress has 60 days to review the accord, but if it votes to reject it Obama can use his veto, which can be overridden only by two-thirds of lawmakers in both houses. That means dozens of Obama's fellow Democrats would have to rebel against one of their president's signature achievements to kill it, an unlikely prospect. Leading Democratic presidential candidate Hillary Clinton called the deal "an important step that puts the lid on Iran's nuclear programmes".
The Senate was not expected to vote on the deal before September.
While the main negotiations were between the United States and Iran, the four other UN Security Council permanent members, Britain, China, France and Russia, are also parties to the deal, as is Germany.
Enmity between Iran and the United States has loomed over the Middle East for decades.
Iran is the predominant Shi'ite Muslim power, hostile both to Israel and to Washington's Sunni Muslim-ruled Arab friends, particularly Saudi Arabia. Allies of Riyadh and Tehran have fought decades of sectarian proxy wars in Syria, Lebanon, Iraq and Yemen.
But there are also strong reasons for Washington and Tehran to cooperate against common foes, above all Islamic State, the Sunni Muslim militant group that has seized swathes of Syria and Iraq. Washington has been bombing Islamic State from the air while Tehran aids Iraqi militias fighting it on the ground.
British Foreign Secretary Philip Hammond told reporters that the deal was about more than just the nuclear issue:
"The big prize here is that, as Iran comes out of the isolation of the last decades and is much more engaged with Western countries, Iranians hopefully begin to travel in larger numbers again, Western companies are able to invest and trade with Iran, there is an opportunity for an opening now."
Still, Washington's friends in the region were furious, especially Israel, whose prime minister, Benjamin Netanyahu, has cultivated a close relationship with Obama's Republican opponents in Congress.
"Iran will get a jackpot, a cash bonanza of hundreds of billions of dollars, which will enable it to continue to pursue its aggression and terror in the region and in the world," he said. "Iran is going to receive a sure path to nuclear weapons."
His deputy foreign minister, Tzipi Hotovely, denounced an "historic surrender" and said Israel would "act with all means to try and stop the agreement being ratified", a clear threat to use its influence to try and block it in Congress.
In phone call with Netanyahu, Obama underscored the United States' commitment to Israel's security, the White House said.
Some diplomats in Vienna said the strong Israeli response could actually help, by making it easier for Rouhani to sell the agreement back in Iran.
The National Council of Resistance, the exile Iranian opposition group that first exposed Iran's secret nuclear programme, said the deal "would neither block the mullahs' pathways to deception nor their access to a nuclear bomb".
While Saudi Arabia did not denounce the deal publicly as Israel did, its officials expressed doubt in private.
"We have learned as Iran's neighbours in the last 40 years that goodwill only led us to harvest sour grapes," a Saudi official who asked to remain anonymous told Reuters.
Nor were hardliners silent in Iran: "Celebrating too early can send a bad signal to the enemy," conservative lawmaker Alireza Zakani said in parliament, according to Fars News agency. Iran's National Security Council would review the accord, "and if they think it is against our national interests, we will not have a deal".
It will probably be months before Iran receives the benefits from the lifting of sanctions because of the need to verify the deal's fulfilment. Once implementation is confirmed, Tehran will immediately gain access to around $100 billion (£64 billion) in frozen assets, and can step up oil exports that have been slashed by almost two-thirds.
The deal finally emerged after nearly three weeks of intense negotiation between US Secretary of State John Kerry and Iranian Foreign Minister Mohammad Javad Zarif - unthinkable for decades, since Iranian revolutionaries stormed the US embassy in Tehran in 1979 and held 52 Americans hostage for 444 days.
Hatred of the United States is still a central tenet of Iran's ruling system, on display only last week at an annual protest day, with crowds chanted "Death to Israel!" and "Death to America!".
But Iranians voted overwhelmingly for Rouhani in 2013 on a clear promise to revive their crippled economy by ending Iran's isolation. Hardline Supreme Leader Ayatollah Ali Khamenei did not block the negotiations.
"New chapter of hope"
"Today could have been the end of hope on this issue, but now we are starting a new chapter of hope," Zarif, who studied in the United States and developed a warm rapport with Kerry, told a news conference.
Kerry said: "This is the good deal we have sought."
European Union foreign policy chief Federica Mogherini said: "I think this is a sign of hope for the entire world."
Tehran has long denied seeking a nuclear weapon and has insisted on the right to nuclear technology for peaceful means. Obama never ruled out military force if negotiations failed, and said on Tuesday that future presidents would still have that option if Iran quit the agreement.
France said the deal would ensure Iran's "breakout time" - the time it would need to build a bomb if it decided to break off the deal - would be one year for the next decade. This has been a main goal of Western negotiators, who wanted to ensure that if a deal collapsed there would be enough time to act.
Obama said Iran had accepted a "snapback" mechanism, under which sanctions would be reinstated if it violated the deal. A UN weapons embargo is to remain in place for five years and a ban on buying missile technology will remain for eight years.
Alongside the main deal, the United Nations nuclear watchdog, the International Atomic Energy Agency, announced an agreement with Iran to resolve its own outstanding issues by the end of this year. The main deal depends on the IAEA being able to inspect Iranian nuclear sites and on Iran answering its questions about possible military aims of previous research.
For Iran, the end of sanctions could bring a rapid economic boom by lifting restrictions that have shrunk its economy by about 20 per cent, according to US estimates. The prospect of a deal has already helped push down global oil prices because of the possibility that Iranian supply could return to the market.
Oil prices tumbled more than a dollar on Tuesday after the deal was reached.