Tehran's martial law administrator, an uncompromising hard-liner detested by the opposition, left Iran today and Shah Mohammad Reza Pahlavi flew out of Tehran for what the palace said was a two-day respite at a nearby resort palace.

Gen. Gholam Oveissi, also the army commander, flew to the United States amid reports he had resigned, apparently sacrificed to the shah's efforts to install a civilian government that would appease the opposition but enable him to remain on the throne.

The nation enjoyed its most peaceful day in two weeks, meanwhile, with some oil workers reported returning to their jobs to produce enough oil for domestic needs and spare the population further fuel shortage.

Before departing, the shah issued a firman , or royal decree, formally naming opposition politician Shahpour Bakhtiar as prime minister to pull together the civilian Cabinet. Aides stressed that he would be back by Saturday, when Bakhtiar is to present the Cabinet for the shah's approval.

Although his palace said the shah had gone only to a royal weekend retreat 40 miles east of Tehran for a much-needed rest with his family, his departure struck political observers as unusual in the middle of the gravest threat to his rule since he returned to power in a CIA-backed coup in 1953.

The idea of a weekend away from it all seemed particularly incongruous in the light of repeated cpposition demands that the shah's departure from the country is the only way to restore order after more than a year of turmoil against his rule.

The shah's opponents, political and religious, are demanding not only that he leave the country, but also that he give up his throne in favor of an Islamic republic. He has given no indication that he is preparing to abdicate, however, and his move from Tehran is likely to be viewed by the opposition as politically insignificant, since he is still reported to be giving orders.

The shah, clinging to what remains of his former absolute power, continued to maneuver in a search for some formula that would placate the militants who are demanding an end to the monarchy.

Radio Iran announced that he had ordered the release of 164 political prisoners in Mashav, in northeastern Iran, a conciliatory gesture to one of the cities that is most committed to his ouster.

In another move designed to calm opposition to his remaining on the throne, the shah appointed Gen. Abeas Gharabaghi as chief of the commander's staff, Iran's highest career military post. Gharabaghi, head of the national gendarmerie, was minister of the interior in the outgoing military government, but well informed sources said he did not participate in the martial law measures that have alienated many Iranians and is not responsible for the army's actions on the street.

The shah passed over Gen Oveissi, the man in line for the job. He was senior to Gharabaghi but as martial law administrator in Iran acquired a reputation as a hard-liner who wanted to use still more force to suppress the shah's opponents.

Gen. Oveissi was reliably reported to have resigned in protest. Ardeshir Zahedi, Iran's ambassador to Washington who has been a close confidant of the shah during the crisis, denied that the general had resigned. He said he had only taken medical leave after an attack of ulcers, but other sources familiar with military affairs said they believe Oveissi had in fact quit.

In Tehran tonight, demonstrators appeared on roofs after curfew to shout their defiance of the shah and there was scattered shooting around the city. In general, however, the country was relatively calm. With freakishly warm weather easing the burden of the fuel strike, interest here has been centered on Bakhtiar's attempt to form a Cabinet.

He is trying to select a government that would have broad religious and political appeal and would be acceptable to enough of the shah's opponents that it could override the demands of the militants for the rejection of any government formed under the shah and for the downfall of the monarchy.

Bakhtiar appears to have retreated from an earlier commitment to form a government only on the understanding that the shah would then leave the country while retaining his title.

There are conilicting reports about how well Bakhtiar has succeeded in forming a government. He was expelled from the National Front and from his own Iran Party for accepting the shah's call to form a government, but he is still trying to entice some National Front members to accept portfolios.

Several lists of potential Cabinet members have circulated through Tehran in the last two days. They agree in general on the presumed appointees, people who have not held any government post under the shah and have good opposition credentials. But there is still doubt about two men who, if they have agreed to serve in the Cabinet, could give the Banktiar government crucial credibility.

They are Nehdi Bazargan, an engineer and university professor with links to Tehran's bazaar merchants and to the shah's bazaar merchants and to the shah's religious opponents, and Kazem Hazssibi, a member of the National Front.

Well-intormed sources in Tehran say both have agreed to take Cabinet posts. But equally authoritative accounts say that the two, who have been in the south negotiating an agreement with oil field workers to produce enough for domestic consumption, promised that they would neverserve in any government under the shah. They are still in the south and could not be reached for comment.