Trump's announcement on social media represented an abrupt turnaround from earlier, when he issued an extraordinary warning that "a whole civilisation will die tonight" if his demands were not met.
Pakistan Prime Minister Shehbaz Sharif, who helped mediate the ceasefire, said in a post on social media platform X he had invited Iranian and US delegations to meet in Islamabad on Friday.
Trump said the last-minute deal was subject to Iran's agreement to pause its blockade of oil and gas supplies through the strait, which typically handles about one-fifth of global oil shipments.
Iran's foreign minister, Abbas Araqchi, said in a statement that Tehran would stop counter-attacks and provide safe passage through the waterway.
"This will be a double sided CEASEFIRE!" Trump wrote on his Truth Social platform. "The reason for doing so is that we have already met and exceeded all Military objectives, and are very far along with a definitive Agreement concerning Longterm PEACE with Iran, and PEACE in the Middle East."
Iran's Supreme National Security Council portrayed the deal as a victory over the US, claiming Trump had accepted Iran's conditions for ending hostilities, while White House press secretary Karoline Leavitt pushed back on that characterisation.
"The truth is that President Trump and our powerful military got Iran to agree to reopening the Strait of Hormuz, and negotiations will continue," she said.
The ceasefire plan includes allowing both Iran and Oman to charge fees on ships transiting through the Strait of Hormuz, a regional official said on Wednesday. The official said Iran would use the money it raised for reconstruction.
In addition to control of the strait, Iran's demands for ending the war include withdrawal of US combat forces from the region, the lifting of sanctions and the release of its frozen assets.
The war, now in its sixth week, has claimed more than 5000 lives in nearly a dozen countries, including more than 1600 civilians in Iran, according to tallies from government sources and human rights groups.
Two White House officials confirmed that Israel has also agreed to the two-week ceasefire and to suspend its bombing campaign on Iran. The agreement includes a cessation of Israel's campaign in Lebanon, Sharif said.
It was not immediately clear how soon the ceasefire would take full effect. Israeli media reported it would begin once Iran reopened the strait and that Israel expected Iranian attacks to continue in the interim.
More than an hour after Trump's announcement, the Israeli military said it had identified missiles launched from Iran, and explosions from intercepted missiles could be heard in Tel Aviv.
Gulf countries including Kuwait, Bahrain, Saudi Arabia and the United Arab Emirates also issued near-simultaneous alerts and activated air defences.
Trump, who has issued a series of threats in recent weeks only to back away, said progress between the two sides had prompted him to agree to the ceasefire.
He said Iran had presented a 10-point proposal that was a "workable basis" for negotiations and he expected an agreement to be "finalised and consummated" during the two-week window.
Markets breathed a sigh of relief, with US stock futures rising in the minutes following Trump's message. Oil prices fell sharply, with US crude futures touching their lowest price since March 26.
With AP