The Supreme Court yesterday upheld the legality of the U.S.-Iranian agreement that led to the release of the hostages.

The unanimous judgment allows the July 19 transfer of about $2 billion in Iranian assets to the government of Iran and to an international claims tribunal. President Carter agreed to return Iran's money, which was frozen in American banks, in exchange for the release of the hostages.

The government also hopes the ruling will avert a foreign policy crisis that might have occurred had the court prevented the United States from keeping its part of the bargain.

In the extraordinary circumstances that confronted Carter, the court said yesterday, American tradition and American law combined to give him the broad authority to act without congressional approval.

The authority was, for a time of peace, as broad as any granted to recent presidents. The agreement nullified lower-court orders, changed the law unilaterally and disposed of billions of dollars. The court said the most relevant judicial analogy was President Truman's effort to seize steel mills, which the court struck down in 1952.

But Justice William H. Rehnquist, writing for the court, stressed that the ruling was narrow and applied only to this situation.

Dames and Moore, a California engineering firm that brought the case, was one of hundreds of companies holding contracts with the government of Iran before the hostage-taking in November, 1979. The crisis left them with no contracts and no payments for any work they might have already done.

Over $8 billion in Iranian funds was in American banks at the time. Carter froze that money in retaliation for the seizure. The companies then sued to get what they considered their share and courts across the country placed holds, or attachments, on some of it pending resolution of the claims.

Under the Jan. 19 agreement, however, Carter agreed to return the money to Iran. All but $2 billion has been returned. Of that amount, a billion is to go to the international tribunal set up to settle outstanding claims.

The companies argued that Carter's action illegally stripped the courts of their power. That, they said, required approval of Congress or at least of two thirds of the Senate.

Because of the impending deadline for transfer of the money, the court accepted the case on an emergency basis and resolved it in seven days, as fast as it has ever handled any major case, delivering its opinion on the final day of its term. A packed courtroom listened for 15 minutes to a tribute to retiring Justice Potter Stewart before Rehnquist announced the results in Dames and Moore vs. Regan, Secretary of the Treasury.

The public needs no reminder of the events that precipitated this controversy, Rehnquist began. "The question presented by this case touch fundamentally upon the manner in which our Republic is to be govered."

We are "acutely aware of the necessity to rest decision on the narrowest possible ground capable of deciding the case," he said.

His 31-page opinion, coupled with a one-paragraph concurrence by Justice John Paul Stevens and a 1 1/2-page limited dissent by Justice Lewis F. Powell Jr., then ticked off each of the companies' arguments against the government.

He said the president had not stripped the courts of jurisdiction over the claims. Instead, the agreement changed the law applicable to the cases. Even though Congress did not approve this, Rehnquist said it was permissible.

The attachments ordered by courts were properly nullified by the president's actions, Rehnquist wrote. His authority to do that came largely from the International Emergency Economic Powers Act, under which Carter declared a national emergency when the hostages were seized.

That law, Rehnquist said, constituted "specific congressional authorization to the president to nullify the attachments and order the transfer of Iranian assets." Its language, the provision saying the president can "nullify, void, prevent, prohibit, transfer" foreign property, is plain, Rehnquist said.

Then Rehnquist addressed the president's power to "suspend claims," the actual court proceedings which were under way to determine whether the companies were actually owned debts by Iran. That is the most difficult question, he said, because nothing in any law specifically allows the president to do what he did.

But many laws regarding foreign disputes imply "congressional acceptance of a broad scope for executive action in circumstances such as those presented in this case." Among those laws are measures enacted in the 1800s and acts passed to help settle post-World War II claims with Yugoslavia, China and East Germany, Rehnquist said.

There is no direct authority for suspending the claims, he said again. But Congress's prior enactments may be considered to "invite" independent presidential action.

That history, combined with the court's own rulings giving the president great authority in foreign affairs, justifies the suspension of the claims, Rehquist said.

The court rejected the companies' contention that the nullification of the attachments violated the Constitution by taking property without just compensation. The companies never actually owned the money being attached, he said, so it can't be taken from them.

Rehnquist said the court would not now rule on whether suspension of the claims -- the court proceedings -- violated the Constitution for the same reason. The companies may still seek their money at the international tribunal, Rehnquist said. If that doesn't work, they can return to the American courts.