Nur Mohammed Taraki resigned tonight as president of the Soviet-backed government of Afghanistan, which has come under increasing pressure in recent months from rebel Moslem tribesmen who control large parts of the rugged, mountainous countryside.

Afghanistan's official radio reported that Taraki, who last week received assurances of greater Soviet support, cited health reasons in turning over the presidency to his number two man, Premier Hafizullah Amin. Considered a more hard-line communist than Taraki, the U.S.-educated Amin emerged several months ago as the country's strongman.

The government change came amid reports that Moscow is trying to broaden the base of the Afghan government, which seized power in a bloody communist coup 17 months ago, in an effort to mollify the rebel Moslems.

However, it was not immediately clear whether Amin, a former student at Columbia University teachers' college in New York, acted at Moscow's behest or tried to solidify his own position against a possible Soviet-backed purge.

The first indications were that Taraki's resignation and Amin's accession to the presidency appeared to take the Kremlin by surprise. The Soviet news agency Tass reported the change after several hours silence on the Afghan developments.

Last month Amin took control of the armed forces following a mutiny by an armored unity in the capital, Kabul.

On Friday, Amin dismissed from his Cabinet two key military officers who helped lead the coup that brought the present government to power.

The two men, Lt. Col. Aslam Watanjar, the interior minister, and Maj. Sherjan Mazdooryar, the minister for border affairs, had been considered by diplomats in Kabul as possible leaders of any attempt to overthrow the Taraki government.

The dismissals were interpreted here as the start of another internal purge aimed at consolidating the power of Amin.

Both officers had retained their popularity in the armed forces despite Afghanistan's worsening guerrilla war, in which gains by Moslem rebels have weakened military morale and caused widespread troop defections. They were the last two military men in the Afghan Cabinet.

Immediately after the dismissals were announced Friday, loud explosions and small-arms fire resounded through Kabul, diplomatic sources reported. They said troops were quickly mobilized when the firing began, but it could not be determined whether there was a connection between the Cabinet purge and the military activity.

It was the first report of widespread gunfire in Kabul since Aug. 5, when troops put down a mutiny by a commando regiment at the capital's Bala Hissar fort.

Watanjar was replaced by a political unknown, Faqir M. Faqir, and Mazdooryar lost his border affairs portfolio to Sahibjan Sahrayee, who held the post before the last Cabinet reshuffle in late July.

According to Kabul Radio monitored here tonight, Taraki submitted his resignation as secretary general of the People's Democratic Party and president of the Revolutionary Council "in view of my health and nervous weakness."

Taraki, 62, former translator for the U.S. Information Service in Afghanistan, returned from Moscow last week after receiving assurances from Soviet leader Leonid Brezhnev of more military and economic aid.

At a rally in Kabul earlier this month, Taraki was hailed as "great leader," "genius leader" and "son of the Afghan people."

He is the founder of the Khalq Party, which had just 4,000 members when it took control after the April 1978 coup. Most of them are urban and middle class, trained in the West and unable to relate to the fiercely independent tribesmen who are devout Moslems.

The rebellion by the tribesmen has been aimed at the government's "godless communists," who they felt would restrict rights to practice the Islamic relition. These tribesmen also have been resisting reform measures imposed by the government.

Although the Soviets have poured more and more military hardware and advisers into Afghanistan since April -- U.S. diplomats reported that between 2,000 and 3,000 Soviets are assisting the Afghan Army -- the rebels have increased their gains in the countryside.

The families of both Taraki and Amin were reported to have gone to Moscow for safety in June.

Although the Soviets have been taking an increasingly active role in the fighting against the rebels, no Soviet combat troops have been reported in Afghanistan so far. Diplomats in Kabul say, however, that Soviet pilots are flying combat sorties against rebel strongholds and bringing Afghan troops to the fighting in giant helicopter gunships.

The Soviets also were reported to have taken control of Bargam Airport, 40 miles from Kabul, where Soviet supply planes land routinely. There have been other reports of Soviet officers taking command roles in the fighting, much as American advisers did in the early days of U.S. involvement in Vietnam.