VIENNA—Iran reached a landmark nuclear agreement with the U.S. and five other world powers, a long-sought foreign policy goal of President Barack Obama that sets the White House on course for months of political strife with dissenters in Congress and in allied Middle Eastern nations.

The accord, which comes after a decade of diplomatic efforts that frequently appeared on the verge of collapse, aims to prevent Iran from producing nuclear weapons in exchange for relief of economic sanctions.

The Obama administration and its partners hope the deal will resolve a dispute that at times threatened to spark a military conflict. In the optimistic view, it would ease tensions with Tehran over time and pave the way for fresh attempts to resolve some of the region’s many other conflicts.

In an address from the White House early Tuesday, Mr. Obama hailed the deal, threatening to veto any vote in Congress against it.

“Today, because America negotiated from a position of strength and principle, we have stopped the spread of nuclear weapons in this region,” he said, adding it wasn't a deal built on trust. “Because of this deal, the international community will be able to verify that the Islamic Republic of Iran will not develop a nuclear weapon.”