A military coup ended the 16-year rule of President Jaafar Nimeri in Sudan today only hours after he had completed a lengthy visit to Washington hoping to shore up his faltering government.

Nimeri, a longtime ally of the United States, was still aboard the Sudanese presidential jet when his defense minister and armed forces commander in Khartoum, Gen. Abdel Rahman Sawar-Dhahab, relieved him and all his aides of their posts early this morning.

According to accounts by the official Sudanese news agency, Sawar-Dhahab said the takeover was for "an interim period" and appealed to the people "to be alert and vigilant to foil attempts to exploit the situation."

The United States considered Nimeri a close and valuable ally ruling the largest country in Africa and a vital strategic link that borders the Red Sea and eight other nations. He was one of the few Arab leaders to support the Camp David accords and the Egyptian-Israeli peace treaty.

[White House spokesman Larry Speakes, in Santa Barbara, Calif., said the "bloodless coup" would not change U.S. policy toward Sudan. He added that there were no reports of any American being injured. A State Department spokesman said diplomatic relations would continue "without interruption."]

It is not clear exactly when Nimeri first heard of the coup, but he appeared to realize that his rule was over when he stopped here on what was supposed to be the last leg of his trip back to Khartoum.

His jet landed shortly before 11 a.m. As he descended the ramp, Nimeri, wearing a gray business suit, was greeted by President Hosni Mubarak and a full complement of Egyptian dignitaries.

They talked for an hour in the presidential reception building at the airport, and Mubarak escorted Nimeri and his entourage back to their plane. But before goodbyes could be said, the crew of the jet informed Nimeri that they would not fly back to Khartoum with him today for "security reasons."

His head bent, Nimeri walked back to the airport and into apparent exile.

Nimeri's presence here could be a liability in the smooth relationship Cairo will hope to establish with the new government, and there was some speculation that he would be asked to leave. But for tonight he was reported staying at the Tahera Palace, described by one Egyptian official as a residence used for "long-stay foreign dignitaries."

Sudan's official Radio Omdurman broadcast a statement from the new rulers pledging to uphold Sudan's regional and international commitments and maintain "blood ties of common destiny with sister Egypt."

A communique from the Army high command said the new government will draft a national program based on the "independence of the judiciary, freedom of expression and the rights of the individual." It said the Army would retain power for a "limited period" before handing it back to "the people."

The coup came as Sudan slid rapidly toward political, economic and military chaos. Since Wednesday, a general strike led by doctors, lawyers and other professionals had cut off all telex and telephone communications with Khartoum.

A rebellion backed by Libyan arms in the non-Moslem southern provinces of the country meanwhile has gained momentum, and the rebel leaders claimed yesterday to be talking secretly with members of the Sudanese armed forces about negotiations excluding Nimeri.

Libyan leader Col. Muammar Qaddafi, who had urged the Sudanese people to revolt, immediately announced diplomatic recognition of Sudan's new military administration, becoming the first world leader to do so. But there was no indication that Libya was directly involved in the takeover.

The coup also was welcomed by Syria's official radio in Damascus. The Saudi government issued a statement saying the coup was "an internal affair," but adding that it "wished success" to the new leaders. Tehran radio, which had been hailing the "uprising of the Moslem Sudanese masses" during the past week, took a cautious approach to news of the coup. A news bulletin said, "There is still no definite information about the affiliation" of those who staged the coup.

In a communique made public by Sudan's official news agency Suna, Sawar-Dhahab said the military had to move because of "the worsening situation in the country and the political crisis that grows worse continuously." The communique said all high government officials were relieved of duty and that the constitution was suspended.

Egypt's Middle East News Agency reported from Khartoum that demonstrators broke into Cober Prison in Khartoum North, a suburb, and "set free all political and nonpolitical detainees." It said most were students and lawyers arrested recently.

There were reports in Khartoum of two persons being killed in a brief gun battle as troops arrived at the state security headquarters. The reports, which could not be confirmed immediately, said there were bursts of machine-gun fire before the soldiers gained control of the headquarters and detained some of the security officers, The Associated Press reported.

[News of the Army takeover was read over Radio Omdurman, which was under heavy guard by soldiers. After the broadcast, demonstrators sang, danced, and shouted anti-Nimeri slogans in the streets. They pulled down the red, green, white and black flags that Nimeri had made the national standard after the 1969 coup, AP said.]

MENA, the Egyptian news agency, said Sawar-Dhahab is 51 years old and was born in Omdurman, across the Nile from Khartoum.

The agency said he was graduated as a second lieutenant from the Sudan Military Academy in 1958, and served in most units of the Army. He was a military attache in Uganda and has had military training in Britain and Jordan.

MENA said he was believed to be a devout Moslem, but not an extremist. He reportedly is a member of the Khatemia sect. He is married and has two sons and three daughters.

Sudan's ambassador to Nairobi, Kenya, Ibrahim Taha Ayub, said of Sawar-Dhahab, "I don't think he has any political ambitions; he is very much an apolitical person without political inclinations."

Mahmoue Tamim, a senior Sudanese diplomat in Nairobi, said there were peaceful demonstrations today in the streets of Khartoum welcoming the change in government.

"There is no information about any arrests up to now," Tamim said. "The only clear thing is that there is going to be a transitional period, an interim period."

Since Nimeri took power in a May 1969 coup, his greatest demonstrated skill has been survival.

By dividing and eliminating his opposition, he made himself appear the only leader capable of keeping order in a country split between an Arab, Moslem north and a black African south where the main religions are traditional animist faiths and Christianity. In 1972 he proved his political skills by negotiating an end to 17 years of civil war between the north and south.

But the multiple crises of the past two years eroded confidence in him on all fronts.

The same man who had made peace in 1972 seemed intent on provoking war in 1983 by trying to impose strict Islamic law on the south.

Ambassador Ayub in Nairobi said the coup leaders may have lifted the sharia, or strict Islamic law, under which offenses such as adultery and possession of alcohol are punishable by sentences ranging from floggings to amputations.

"The sharia was incorporated in the constitution, and since the constitution was suspended I think that the sharia law is also suspended," he said.

In late 1984, Washington temporarily suspended more than $194 million in economic assistance to Sudan because of the deteriorating situation there.

On March 4 Vice President Bush visited Khartoum and greeted Nimeri as an "old friend" but avoided any public commitment to a renewal of aid.

A few days later, Nimeri began one of the sudden political turn-arounds that were his trademark.

Having worked closely with the fundamentalist Moslem Brotherhood for the past two years and having brought some of its leaders into senior positions in his government, he now purged them and blamed them for many of Sudan's current problems.

What began the current crisis in Khartoum, however, was Nimeri's announcement the day he left for Washington that subsidies on basic commodities would end and the currency would be devalued to meet U.S. and International Monetary Fund demands about how the economy should be run.

Rioting broke out immediately in Khartoum, and the strikes that shut down the city this week began to build.