The International Court of Justice, in a strongly worded decision, ruled unanimously here today that Iran should release all of its American hostages and return the U.S. Embassy in Tehran to American control.

The ruling by the World Court, whose membership includes representation from a broad range of Western, communist and Third World countries, appeared to lay the legal and moral groundwork the United States had hoped for to allow it to seek economic or other sanctions against Iran through the United Nations.

While the court has no means of enforcement and relies on voluntary compliance, its decisions carry a great deal of weight since the panel is the main arbitration arm of the United Nations.

Attorney General Benjamin Civiletti said in Washington that the court's decision "couldn't have been a better victory." He said it "completely supports the position of the United States and the position of all civilized peoples."

Civileti, speaking to reporters at the White House, said that if Iran defied the ruling by the World Court, the United Sates would feel free to consider a "wide range of alternatives." He gave no details.

The court's stern order said that "there is no more fundamental prerequisite for the conduct of relations between states than the inviolability of diplomatic envoys and embassies, so that throughout history, nations of all creeds and cultures have observed reciprocal obligations for that purpose."

The obligations to assure the personal safety of diplomats and their freedom from prosecution are, the court said, "essential, unqualified and inherent in their representative character and their diplomatic function."

The court indicated that it would not hesitate to speak out again on the matter, even without being approached by the United States or Iran, saying that it "will keep the matters covered by this order continuously under review."

The ruling also seemed to put the United States on gurard against any military intervention against Iran, saying that neither government should "take any action and should insure that no action is taken which may aggravate the tension between the two countries or render the existing dispute more difficult of solution."

The 17-page ruling was read by the British president of the court, Humphrey Waldock. In addition to the United Sates and the major Western allies, the court also includes judges from the Soviet Union, Poland, Syria, Egypt, India, Nigeria and Senegal.

As on Monday, when Civiletti led pleas for a speedy ruling, the chairs reserved for Iran, as a party to the action, stood empty.

In a telegram to the court, Iran's Islamic revolutionary government had called the dispute over the 50 Americans who were seized at the embassy Nov. 4, a "marginal and secondary element" in the broader dispute about 25 years of "shameless" U.S. "exploitation" and interferences" in Iran.

The court's decision explicityl rejected the Iranian view that there is anything secondary about the Tehran embassy situation. It noted that the United Nations has called on Iran to free the hostages and return the embassy to U.S. control.

Chief Judge Waldock noted that he and two other judges had each sought additional information from Stephen M. Schwebel, State Department deputy legal adviser and the chief U.S. representative here during the week. U.S. lsources said that the points raised by the judges appeared in today's ruling in a way that strengthened the American position beyond the U.S. legal team's own arguments.

The Americans had skirted the issue of the status of the two private Americans held at the Tehran embassy, and the status of the U.S. charge d'affaires, Burce Laingen, and his two colleagues held separately at the Iranian Foreign Ministry.

In a more detailed breakdown of the official positions of all 53 Americans held in both places that the U.S. government has provided, the court seemed to be seeking to establish the precedent that all embassy staffers and private citizens on diplomatic premises are entitled to protection.

The court also went out of its way to stress that consular officials enjoy the same privileges as those officially recognized as diplomats by host governments.

The court listed the hostages as 28 persons recognized as diplomats by Iran, including four consular officials, 20 administrative and technical staff members, two diplomats held at the Foreign Ministry under circumstances that are "a treat to their inviolability as diplomats."

Technically, the court's ruling is the equivalent in American law to a temporary injunction or a cease and desist order pending a final decision that could take years.

The U.S. legal arguments sought both today's results and an eventual formal finding against Iran that would include the payment of damages and a finding on the legality of the Iranian action. Theoretically, today's ruling was not meant to pass on the question of legality, but the court's reaffirmation of diplomatic usage had almost the same effect.

Apparently not wanting to give the impression that it was taking political sides against Iran, the court also invited the Tehran government to state its case against U.S. actions in Iran over the years.

Although Iran was not represented in this week's proceedings, its telegram to the court was couched in the terms of the highest respect for the international tribunal.

Some observers speculated that Iran might possibly accept the court as the body to conduct the interntional investigation the Islamic government has demanded into American support of the shah.

The court's ruling apparently demonstrated reasons to accept such a role, saying that today's ruling does not affect Iran's right to submit arguments on the U.S. complaint or to file a counterclaim.

But not appearing in the present proceedings, the government of Iran, by its own choice, deprives itself of the opportunity of developing its own argument before the court," the ruling said.