With U.S. and 10 other western diplomats lending their support to peaceful protest in South Africa, more than 40,000 people attended a funeral in this black township outside Pretoria today for 12 persons killed in a clash with police two weeks ago.

Adding her voice to numerous appeals for black rights was Winnie Mandela, wife of imprisoned leader Nelson Mandela, who appeared briefly in defiance of restriction orders prohibiting her from attending or addressing gatherings.

"We are here today as testimony to the failure of the government to rule our country," Mandela said. "We are here as testimony to the fact that the future of this country lies in black hands."

The colors and slogans of Nelson Mandela's outlawed African National Congress were everywhere. A huge black, yellow and green ANC banner was carried at the head of the funeral procession and similar flags were draped over the coffins. As the bodies were carried from a soccer stadium, where the rally was held, the crowd stood with fists raised in the salute of the ANC and sang in praise of its guerrilla wing, called Spear of the Nation.

The ANC is the most potent symbol of black militancy for both whites and nonwhites in this racially divided country. As the ANC's presence was felt here, President Pieter W. Botha announced in Pretoria, only 12 miles away, that he was lifting the state of emergency in eight of the 38 designated areas since the revolutionary climate in the black townships was "fast losing momentum."

The diplomats were among a sprinkling of whites to attend the funeral. It was the first official U.S. attendance at the numerous funeral rallies during the 15 months of conflict. The Detainees' Parents Support Committee today put the total of persons killed in that time at 908.

Tim Carney, a political counselor at the U.S. Embassy, said it was decided he should attend "because we have friends here and some of our employes live here," and also because "we are underscoring our firm support for the principle of peaceful protest."

Diplomats from Britain, West Germany, France and seven other countries attended in what appeared to be a coordinated presence. These countries have expressed unease at the pace of change and at government's clampdown on political expression.

On Nov. 21, police opened fire on a crowd of 50,000 demonstrators, mostly women, outside Mamelodi's administrative offices, causing a stampede in which 13 people were killed and several hundred injured. One of the dead was buried in another township today.

Mamelodi residents charge that they were fired on from a helicopter as well as from armored personnel carriers, but police have denied this, saying the helicopter was used only for crowd observation.

Residents have begun a boycott of white-owned shops in Pretoria in protest, and city employers reported an 80 percent response to a call for a work stoppage to mark today's funeral.

The funeral rally went ahead after two weeks of negotiations ended in a deal between a residents' committee and the police -- who agreed to keep a low profile if community leaders kept the rally orderly.

The deal also allowed the press to enter the township for the first time since restrictions were imposed on the reporting of racial unrest a month ago.

A heavy contingent of troops kept watch from an encampment on a hillside just outside the township, with two helicopters standing by, but there was no sign of any disturbance throughout the five-hour rally.

Members of the committee that negotiated with the police made frequent appeals for orderliness. "Let us show the world that we can behave like people made in the image of God," one of the committee members implored the crowd.

White groups that attended were loudly cheered, and several whites, including veteran opposition member of Parliament Helen Suzman, were invited to speak.

An undercurrent of radicalism was evident, with chants in praise of the South African Communist Party, the Nicaraguan Sandinistas and Marxist regimes in Africa. One speaker urged the crowd to follow the revolutionary examples of Vietnam, Mozambique, Angola, Ethiopia and Iran.

However, the open support for the ANC was the rally's dominant theme. Vendors sold small lapel ribbons in the outlawed movement's colors. ANC leaders were praised in songs and chants. A youth leader, Chris Ncobo, was loudly cheered when he declared: "We all know that the ANC is the pride of our people."

There was particularly warm applause for Martha Mahlangu, mother of ANC guerrilla Solomon Mahlangu, who was executed in 1979 after being wounded and captured in a gunfight with police in downtown Johannesburg.

"The struggle must go on; the power is ours," Martha Mahlangu said, addressing the crowd in her Tswana language and speaking in a shrill, angry voice.

Winnie Mandela's appearance, shortly before the graveside ceremony ended, took the crowd by surprise. She was mobbed by enthusiastic people, who insisted on returning to the stadium for her to address them.

About 8,000 returned to hear Mandela, who said she brought them "messages of love from your leaders in prison who share your pain."

Mandela's act of defiance, the latest of a series, could lead to her being imprisoned for violating a range of restriction orders under which she has lived since her husband was sentenced to life imprisonment 23 years ago.

Mrs. Mandela has been defying the restrictions for the past two months, since gasoline bombs destroyed her house in the remote village of Brandfort to which she was banished eight years ago.

She left Brandfort without police permission after the bombing and has refused to return, despite official letters setting a deadline of Nov. 4 for her to do so. She has traveled the country freely and addressed several press conferences.

Mandela appears to be taking a calculated risk, basing her defiance on an assumption that the authorities will be reluctant to take action against her at a time when pressure for her husband's release is building up inside South Africa and abroad.

Botha today lifted the state of emergency in five towns in eastern Cape Province and three in Transvaal Province. All have been on the fringes of major trouble zones.

The president said the government believed radical elements who were behind the unrest were now being defeated. He said the unrest had been instigated by outsiders who were opposed to the government's program of "orderly reform" and had tried to drive communities in South Africa toward a violent confrontation in order to further their own radical cause.

"It has recently become evident that the revolutionary climate instigated from without our borders among people in South Africa is fast losing momentum," Botha added.