A South African judge granted an unprecedented last-minute stay of execution today to a black prisoner who was due to go to the gallows at dawn Wednesday, following diplomatic pressure from the United States and Britain.

The act of clemency, which put back the execution date three weeks to allow the condemned man to petition President Pieter W. Botha, came as the South African authorities otherwise continued to deal sternly with the country's racial crisis. The police raided five black townships, including a section of Soweto, the sprawling ghetto where most of the Johannesburg area's 2 million blacks live.

Troops cordoned off two of the five black areas, and armored vehicles lined the streets while the police conducted house-to-house searches, arresting scores of people under a martial law proclamation issued three weeks ago.

A judge granted the stay of execution for Benjamin Moloisi, a supporter of the underground African National Congress convicted of killing a black security policeman three years ago, after his lawyers petitioned the provincial Supreme Court in Pretoria today. However, the ruling bore all the signs of a government response to strong diplomatic pressure.

Botha rejected a plea for clemency only last week. Following diplomatic moves in which the United States is understood to have made representations to the South African Embassy in Washington and to Botha and the ministers of justice and foreign affairs in Pretoria, defense lawyers said official sources suggested today that they make an urgent application for a stay of execution to allow time for a new petition to Botha.

The application, made late this afternoon by one of the country's top lawyers, Isaac Maisels, was granted by Judge C. Eloff, who said that he did not want to establish a precedent by ordering a last-minute stay of execution but believed that the application -- based on a sociologist's report on Moloisi -- was "not so devoid of the possibility of success that I cannot say it won't be accepted."

In Washington, Robert L. Bruce of the State Department's Bureau of African Affairs confirmed that the United States had taken up the Moloisi case "recently" with South African officials "both here and in Pretoria." Bruce refused to elaborate, except to note that the United States had voted in favor of a 1984 United Nations resolution urging South Africa not to carry out the execution.

[National security affairs adviser Robert C. McFarlane also mentioned the scheduled execution in answer to a question at a civic group meeting in Santa Barbara, Calif., saying he thought it would worsen the situation.]

News of the stay of execution came tonight while Moloisi's mother, Pauline Moloisi, 53, was leading prayers at a planned all-night vigil in Johannesburg's Christ the King Roman Catholic cathedral.

A crowd of several thousand blacks, with a sprinkling of white sympathizers, cheered, stamped their feet and sang black freedom songs as the priest, Andrew Kelly, interrupted the service to announce the news.

Fears had been expressed that the execution of Moloisi, 31, could spark a fresh outbreak of unrest in South Africa's tense racial atmosphere.

He would have been the fifth member of the African congress, which is trying to overthrow white-minority rule in South Africa by guerrilla struggle, to go to the gallows. The others have become revered symbols for many in the black community.

Pauline Moloisi, 53, made an appeal Saturday to the international community, particularly the United States and Britain, to pressure the South African government to spare her son's life. She protested his innocence, noting that ANC headquarters in Lusaka, Zambia, had issued statements saying he was not a member of their hit squad that killed the security policeman -- something the underground movement has never done when its members have been convicted.

Priscilla Jana, a lawyer for Moloisi, also said Saturday that there had been serious irregularities at his trial. She said Moloisi had been made to write a confession while detained incommunicado and had been required to enter a plea to the charge of murder and to outline his defense before being allowed to see his lawyers.

Britain responded to Pauline Moloisi's appeal Monday by making representations to the South African Embassy in London and the Department of Foreign Affairs in Pretoria. It is understood the United States made its representations today.

The police raids today took place in Zone One Diepkloof, a sector containing 2,000 residences, in three black townships near Witbank in eastern Transvaal Province, and another called Thokozo, outside the city of Germiston, southeast of Johannesburg.

According to eyewitnesses, more than 100 Casspir armored personnel carriers with policemen and troops drove into Diepkloof at dawn. The armed men threw a cordon around the township zone and, while an armed soldier stood outside each door, a squad of police searched the little huts and questioned their occupants.

Soldiers stopped motorists to search them and their cars, demanding to see driver's licences and the reference books blacks must carry at all times to certify that they are allowed to be in an urban area instead of a tribal "homeland." Taxis and buses were also stopped and their occupants searched. Many people were arrested and taken away, but police headquarters in Pretoria refused tonight to say how many.

After the searches, each person was marked with a red dye on the forehead or a hand to show he had been cleared.

Ntatho Motlana, a doctor who is chairman of the Soweto Civic Association and has an office in Diepkloof, said the police action "was pure provocation. There was no sign of trouble, nothing; they just came in to make a show of force, and some of them behaved like cowboys, all arrogance." TEXT OMITTED