It was in many ways the diametrical opposite of Mr. Trump’s version. Iran is not meddling in the Middle East, he said, it is helping neighbors who ask for help. Iran is not sponsoring terrorism, he said, but fighting it. Iran’s missiles are not meant to attack but to defend.

And Mr. Rouhani said the nuclear agreement, which was negotiated not just with the United States but five other major powers — Britain, China, France, Germany and Russia — was a model of international diplomacy that should be emulated, not abandoned.

“I declare before you that the Islamic Republic of Iran will not be the first country to violate the agreement; but it will respond decisively and resolutely to its violation by any party,” he said.

In an indirect but pointed swipe at Mr. Trump’s administration, he also said “it will be a great pity if this agreement were to be destroyed by ‘rogue’ newcomers to the world of politics: the world will have lost a great opportunity.”

Mr. Trump’s portrayal of Iran as a fomenter of Middle East conflict, Mr. Rouhani said, was misplaced, given America’s 2003 invasion of Iraq and other military operations in the area.

“The United States government should explain to its own people why, after spending billions of dollars of the assets of the people of America and of our region, instead of contributing to peace and stability, it has only brought war, misery, poverty and the rise of terrorism and extremism to the region,” he said.

Later at an hourlong news conference, Mr. Rouhani held open the possibility of resuming uranium enrichment that is restricted under the nuclear agreement, should it unravel. But he also reiterated Iran’s contention that it does not want, and will never seek, nuclear weapons.