After more than three weeks of intense international pressure and Palestinian unrest in the occupied West Bank, the Israeli government today reversed itself, freeing imprisoned Nablus Mayor Bassam Shaka and dropping deportation proceedings against him.

The arrest of Shaka on the basis of a disputed allegation that he expressed approval of a 1978 terrorist massacre touched off anti-Israeli demonstrations and tensions throughout the occupied territories.

Shaka, who has been imprisoned since Nov. 11, can resume his mayoral post immediately, military government authorities ruled, but they ordered the outspoken supporter of the Palestine Liberation Organization to limit his activities to municipal matters.

Shaka returned to cheering crowds in Nablus tonight and declared, "Justice triumphed!"

The other mayors in the West Bank, who collectively had resigned in protest over Shaka's arrest, said they would withdraw their resignations and return to their jobs.

Israel's about-face decision defused a steadily worsening crisis in the West Bank that had led to a series of general strikes and occasionally violent clashes between security forces and Palestinian youths.

The handling of the expulsion order had also aroused controversy in Israel.

Shaka appealed the expulsion to the Israeli supreme court and the court, after initially blocking his expulsion, had agreed to hear the case in detail after the military review was completed.

The military governor of the West Bank, Brig. Gen. Binyamin Ben-Eliezer, said he weighed many factors, including "the needs of the city of Nablus and Mr. Shaka's family," before deciding to drop the expulsion order. Ben-Eliezer said he also took into account declarations by Shaka that he is opposed to "acts of murder of innocent victims."

Ben-Eliezer said he had "made it clear to Mr. Shaka to limit himself to his functions as mayor and to act within the framework of the law." He said Shaka understands the conditions.

Government sources said Defense Minister Ezer Weizman decided to release Shaka before leaving on a visit to Britain yesterday, and that he consulted members of the Cabinet's defense and security committee, including Prime Minister Menachem Begin.

The decision was relayed to Cairo by "hotline" telephone because Egypt had protested on Shaka's behalf.

Although Ben-Eliezer refused to characterize the ruling as a retreat by the military government, the mayors of leading West Bank and Gaza Strip towns hailed it as a victory.

Gaza Mayor Rashid Shawa said the government has "acted on the wishes and accepted world public opinion that asked for the release of Bassam Shaka."

The U.N. General Assembly voted overwhelmingly in support of a resolution favoring Shaka, and Secretary of State Cyrus Vance wrote Begin asking that Israel reconsider the order.

Bethlehem Mayor Elias Freij called the decision "wise and courageous," and said the resignations of the 25 other mayors would be withdrawn. Some West Bank towns have experienced serious financial problems following Jordan's refusal to supply its customary financial aid in the mayors' absence.

Shaka had been accused of saying in a private conversation with an Israeli military official, Gen. Danny Matt, that he approved of the terrorist attack in March 1978 on the Tel Aviv coastal road, in which 34 Israelis and 9 Arab commandos died.

A transcript of the conversation, however, showed that Shaka had said he did not identify with such terrorist acts but had warned they were to be expected, given Israel's 12-year occupation of the West Bank and Gaza Strip.

Ben-Eliezer rejected the notion that the controversy reflected a test of wills between Palestinian leaders and the Israeli military government. "I see this decision as a proof of strength rather than weakness," he said. "I believe this is proof of strength to be able to make unpleasant decisions."

The ultranationalist Gush Emunim movement condemned the decision, saying there is "no limit to the capitulation of the government to the Palestine Liberation Organization."

Meanwhile, another crisis was averted or at least delaying when the ruling body of the ultraorthodox party, the Council of Torah Sages, decided to give Begin's Likud coalition two more weeks to pass legislation prohibiting abortions for social or economic reasons. The four parliamentary members of another faction, Agudat Yisrael, had threatened to quit the coalition. That would have left Begin's shaky Likud with a one-vote margin in the 120-member parliament and probably led to the collapse of the government.

The Torah Council's decision followed an announcement by Begin that he will turn the abortion vote into a vote of confidence in the government.

This will force all members of the coalition to vote in favor of the restrictive abortion legislation, thereby improving its chances. The last vote on the measure was deadlocked because of defections by coalition members.