South Africa admitted today that it has troops secretly stationed in northern Angola.

The admission, by Defense Force chief Gen. Constand Viljoen, came after Angola announced that it had killed two South Africans and captured a third who were trying to sabotage an oil refinery in the enclave of Cabinda, north of Luanda, yesterday. The refinery, near Malongo, belongs to an American company, Cabinda Gulf Oil.

The development could cause political embarrassment both for the South African government and the Reagan administration, in the view of political analysts here.

The analysts also said that the disclosure held serious implications for the prolonged and delicate peace negotiations in the region and that it was likely to prejudice Washington's role as mediator in these negotiations.

This was because it meant South Africa had violated the Lusaka agreement that the United States negotiated 15 months ago between Pretoria and Luanda, which Washington regarded as a basis for building mutual trust between the two chief antagonists in the long regional dispute.

South Africa completed what purported to be the withdrawal of the last of its troops from Angola under this agreement just five weeks ago.

"This will be an embarrassment for the U.S. administration," said John Barratt, director of the South African Institute of International Affairs in Johannesburg.

"Either they'll have to say that they didn't know anything about it, or that they did know and didn't do anything," Barratt said.

The South African Defense Force initially denied any involvement in the Cabinda incident, but a few hours later Viljoen issued his statement admitting that South Africa has troops in the area.

He still would not confirm that these troops had clashed with the Angolans, saying only that there was concern because contact with the troops had been broken.

In his statement, issued from military headquarters in Pretoria, Viljoen said: "The Defense Force is involved in gathering information about hostile elements which threaten the safety of South-West Africa Namibia and South Africa."

He said the Defense Force had deployed small groups of soldiers north of South Africa's border to gather information on the South-West Africa People's Organization (SWAPO), which is fighting to wrest Namibia from Pretoria's control, the African National Congress, which is the main black South African underground movement, and "Russian surrogate forces."

"A small element was gathering information about ANC and SWAPO bases, as well as Cuban involvement with them, in the area south and north of Luanda," Viljoen said.

Both SWAPO and the ANC have their main guerrilla training bases in Angola, and it is estimated that the Luanda government has 25,000 Cuban troops in its territory, ostensibly to protect it from attacks by South Africa and insurgents of Jonas Savimbi's Union for the Total Liberation of Angola (UNITA), which South Africa supports.

Political analysts and opposition politicians were sharply critical of what one described as a "reckless adventure" in sending troops more than 1,000 miles into Angola.

Frederik van Zyl Slabbert, leader of the main opposition party, the liberal Progressive Federal Party, said the incident "must of necessity reflect on South Africa's international credibility."

Others noted that "information-gathering" was normally done by intelligence services, not soldiers.

Barratt, the institute director, suggested that the disclosure would undermine U.S. demands that Angola must withdraw all, or most, of the Cuban troops from its territory before there can be a settlement in neighboring Namibia.

Angola has protested that it needs the Cubans to protect it from South African attacks, to which the United States has responded that South Africa's withdrawal under the Lusaka agreement has removed this threat. Assistant Secretary of State Chester A. Crocker recently proposed a compromise based on this proposition to Angola and South Africa, and is waiting for them to respond.

"There is no doubt that this puts Crocker in a very difficult situation," Barratt said