Racial violence erupted in downtown Johannesburg for the first time today, when thousands of blacks rampaged through the streets after a memorial service for a black activist executed this morning despite a barrage of international pleas to the South African government to spare his life.

A surge of emotion that swept the black community after Benjamin Moloisi, 30, was hanged at dawn also sparked renewed violence in troubled Cape Town, and for the first time there were reports of protesters shooting back at police trying to disperse them with shotguns and tear gas.

A white policeman was shot in the stomach and another in the leg as further clashes broke out last night in Cape Town's mixed-race township of Athlone, while three white policemen were wounded with knives during the rioting in Johannesburg today.

Whites in Johannesburg appeared startled by today's rioting, for most of them the first sight they have had of the unrest that has been imploding in the country's segregated black townships during the past 14 months.

With a young white soldier who was stabbed to death in a black township near Port Elizabeth Sunday, the casualties last night and today are the first suffered by whites in the security forces during the unrest, in which more than 750 blacks have been killed and thousands injured.

Moloisi, an admitted supporter of the outlawed African National Congress who was condemned for participating in the assassination of a black security policeman, was executed in the face of an unprecedented number of pleas for clemency from around the globe.

The refusal to extend clemency to Moloisi indicated a change of mood in Pretoria, observers said, that seems to have developed during the past two months -- between the granting of a stay of execution for him Aug. 20 after representations by the United States, which seemed to indicate that he was going to be reprieved, and an announcement Tuesday that President Pieter W. Botha had rejected a formal petition for clemency and a reopening of the trial to hear new evidence.

There were emotional scenes this morning outside Pretoria Central Prison, where Moloisi was hanged, as relatives and the condemned man's mother, Pauline Moloisi, gathered there and sang the black African anthem, "God Bless Africa."

The small crowd was joined by Winnie Mandela, wife of imprisoned congress leader Nelson Mandela, who defied her restriction orders to demonstrate her support for Moloisi.

Mandela defied the restrictions again when she appeared with Pauline Moloisi and spoke briefly at a lunch-hour memorial service for the executed man in downtown Johannesburg.

It was after this service that the violence erupted.

As the crowd of about 500 emerged singing from the building where the service was held, a few whites living in second- and third-floor apartments across the street began pelting them with tomatoes and flowerpots from their balconies.

The enraged mourners hurled bottles and other objects back at the apartment-dwellers. As other blacks quickly joined them, swelling groups began rampaging through the streets attacking white pedestrians, smashing shop windows and looting stores.

The rioting, which continued sporadically for 2 1/2 hours and ranged over six city blocks, has been described as the first instance of racial mob violence in downtown Johannesburg.

Some whites confronting today's violence fled in terror; others clashed with the rampaging youths and fistfights broke out. Most looked on, seemingly bewildered by the unaccustomed sight.

As police tried to arrest some of the rioters, three white policemen were stabbed, one seriously. At least one black man was injured seriously when police tried to quell the rioting with shotguns, and a number of others were injured and arrested. A number of pedestrians and shop workers also were injured, none seriously.

Another memorial service for Moloisi in Cape Town's black township of Guguletu also ended in running street battles with the police. Ten blacks were reported injured.

But the main violence in Cape Town was again in the mixed-race township of Athlone, where for the fourth consecutive day local residents clashed with the police, who reported that some protesters had opened fire on them with handguns and rifles.

Two white policemen were shot during a bitter clash in Athlone last night, when police opened fire on a crowd of about 6,000 residents leaving a protest meeting in a mosque.

One man of mixed race was killed by the police gunfire and several were injured. About 2,000 persons were trapped in the mosque for three hours by the shooting.

The meeting in the mosque had been called by the Moslem Judicial Council to demand the release from police custody of the bodies of three persons killed on Tuesday when security forces opened fire on demonstrators from the back of a truck. The withholding of the bodies has angered the Moslem community, whose faith requires that the dead be buried swiftly.

As the violence continued in Athlone today, youths set up street barricades of burning tires and set fire to a number of vehicles.

Large contingents of troops and police moved into Athlone and other Cape Town townships late yesterday in what looked like the start of a massive operation to try to crush the persistent unrest there. Reporters at the scene described it as the biggest security force operation in the region so far.

The hardening mood on the part of the government in Pretoria has come, observers said, in the face of mounting international and local pressures to dismantle the apartheid system of segregation at a much faster pace.

This is being accompanied by a toughened attitude toward the African National Congress as President Botha feels himself being pressured to begin negotiations with the black nationalist organization, which his ruling white Afrikaner-dominated National Party outlawed 25 years ago.

This attitude was revealed earlier this week when the government seized the passports of eight students at Stellenbosch University -- the ruling Afrikaner community's most respected academy -- after they announced that they intended to travel to Zambia to meet members of the congress' youth league. Justice Minister H.J. Coetsee followed this up with a warning that South Africans who had meetings with the congress might be charged with treason.