By the time the first canister of tear gas exploded, the Ku Klux Klan was on its way out of town.

But anti-Klan demonstrators quickly found a new target, their protest transformed into a spontaneous outburst against police that spread into sporadic looting.

For hours the rhetoric had been building as some 500 demonstrators, including hundreds of Palestinian sympathizers, marched from the Ellipse to McPherson Square and an additional 5,000 anti-Klan protesters rallied near the Capitol.

"You don't know who the Klan is. It may be your landlord. It may be the merchant you buy your food from. It may be your insurance man," Ethel Mathews of the Georgia Welfare Rights Organization warned the crowd at McPherson Square. "We are here to say . . . that we are not afraid."

Other speakers decried federal budget cutbacks, unemployment and U.S. support for Israel and South Africa.

By the time the two groups of protesters converged near Lafayette Square, the atmosphere was ripe for confrontation. It came suddenly, as the two crowds tried to push toward the park where they believed the Klan was congregated.

Officers hustled the Klansmen away, then, with helmets and nightsticks at the ready, formed a solid phalanx in front of the park, turning back a large group.

It was never clear what sparked the first blow -- police said the crowd began throwing rocks when they were turned away; some protesters ran through the crowd shouting that police were beating people, though it could not be determined what incident they were referring to.

But one thing was certain: the crowd's mood changed. The chant of "Reagan and the Klan work hand in hand" quickly changed to "The police and the Klan work hand in hand." Bricks, stones from the street and bottles flew in one direction; tear gas came back.

While many white police officers were pelted with rocks, black policemen bore the brunt of the crowd's anger. "You're just doing whitey's job. You ain't s---," called one black women, her eyes watering from tear gas, to a black park policeman on horseback.

For many, the clash appeared to represent genuine frustration over the presence in Washington of a potent symbol of racism and hatred. "If people just let them walk through they might think people are afraid of them, but people aren't afraid of them anymore," said Louis Wilson of Wheaton, Md., who had brought his 4-year-old daughter to the rally.

"I couldn't believe the KKK was really supposed to be here," said Gayle Smith of Alexandria, Va. "I think people have ignored them long enough and they haven't gone away."

Some of the demonstrators belong to groups that have a history of deliberately seeking physical confrontations with the Klan and Nazi groups. The Progressive Labor Party, for example, has repeatedly attacked Klan rallies in other cities in recent years and has vowed publicly to assault Klan and Nazi members.

Others in the crowds yesterday seemed to see the day's events as simply a cover for individual violence and crime. It became an opportunity for some youths to operate anonymously, occasionally bolting from the crowd to break into bicycle and camera shops, then disappearing back into the throng.

Individuals would from time to time move up behind a crowd of protesters, throwing rocks over their heads toward the police or toward windows of businesses and then flee, laughing as if it were a game, when the police came after them.

Police Chief Maurice Turner estimated that half of the 38 arrested yesterday had previous criminal records.

While the crowd of demonstrators, black and white, mostly in their 20s and 30s, included many who had come from as far as New York, Boston, New Jersey and Cleveland, many of those arrested yesterday gave local addresses.

Organizers of the anti-Klan protests pleaded unsuccessfully for an end to the violence. "The rally is over, take down your banners before we get hurt. The rally is over," exhorted Al Nelson, an organizer for the Spartacist League, the major group behind the demonstration at the Capitol.

He said later, "This wasn't planned. A few people were hurt before we were able to get control."

Before the violence broke out, some of the rally's organizers criticized Turner's public call for people to stay away from yesterday's demonstrations, and complained about the way the events were being handled. "They the police are out protecting the Klan, but no one is talking about safeguarding us," complained Sahu Barron, a national coordinator of the All People's Congress.

After his 6 p.m. press conference, D.C. Police Chief Maurice Turner was asked whether the department's decision to isolate the Klan marchers contributed to the later violence.

"Possibly so," said Turner, "I can't say one way or the other. But I think that a lot of these people were frustrated and took out their frustration on the police and other city agencies."

"People aren't just upset at the Klan and the police," said George Giles, a steelworker who came to the rally from Cleveland. "They're frustrated. They're out of work and they want to take it out on somebody."

In an alley behind an office building on 15th Street a black man in his mid-30s, shouting about not being able to find a job, broke all the windows of a car parked in the alley and stole a basketball from it. Then someone set fire to a garbage bin nearby.

"There was a lot of emotion about the Klan," Mayor Marion Barry said late yesterday of the day's violence. "Under the circumstances, it turned out all right. It could have been a lot worse . . . There could have been more destruction."