A troubled Mayor Marion Barry, buffeted this week by devastating testimony and evidence in his drug and perjury trial, has sought refuge among several black leaders who have fought battles along starkly racial lines.

With many of his more traditional supporters abandoning him, Barry has enlisted support from Nation of Islam leader Louis Farrakhan, Bishop George Augustus Stallings Jr., Rep. Gus Savage (D-Ill.) and other black activists who have portrayed the mayor as the victim of a racist plot by the government.

Yesterday, Stallings warned that "we are going to have hell on Earth" unless U.S. Judge Thomas Penfield Jackson permits Stallings and other prominent Barry advocates to attend the trial. Jackson has barred Stallings and Farrakhan from the courtroom, saying that their presence would be disruptive and possibly intimidating to the jury.

"It is clearly obvious that the judge is working in concert with the prosecution," Stallings told reporters outside the courthouse. " . . . The judge must be well aware that he will only increase the racial tension."

During an extraordinary week of courtroom drama in which the FBI videotape of Barry smoking crack cocaine at the Vista Hotel Jan. 18 was released, the mayor appeared twice on stage with Farrakhan, who pronounced Barry a "repentant" politician who should seek another term.

Stallings, who broke with the Catholic Church to establish the Imani Temple African-American Catholic Congregation, and Savage, who has accused the "white press" of distorting his record, each appeared once on stage with Barry and Farrakhan at the Washington Convention Center.

Farrakhan, Stallings and Savage all have been embroiled in highly charged racial controversies, prompting one Barry supporter to suggest yesterday that the mayor is playing an "obvious racial card."

"I think he's trying to solidify himself in the African-centric community," said Howard Croft, a longtime Barry supporter and member of the D.C. Parole Board. ". . . I don't think it has a very broad base at all, but it may be the only activist group he can tie in to," Croft added.

"He's going to where the crowds are," said one former Barry supporter who was there. "Barry's been evidently unable to generate large crowds of his own lately . . . . He still wants to be that public presence. He still wants to feel important."

"I think he is searching for some affirmation of himself," said Sharon Pratt Dixon, a Democratic candidate for mayor. "I think Marion will seek whatever support he can find from whomever and however."

Others suggested that Barry, who has said he wants to exert some influence in the mayor's race, is trying to salvage the vestiges of his hard-core black support.

"I think he's out campaigning for something," said Max N. Berry, a onetime Barry supporter who is now backing Democrat Charlene Drew Jarvis in the mayor's race. "It may just be his image."

Yesterday, in a brief meeting with reporters, Barry hammered away at the judge's exclusion of Farrakhan and Stallings from the courtroom, saying that it smacks of totalitarianism.

"It's like in Nazi Germany," said Barry, accompanied by his wife, Effi.

"This trial is not just about some alleged criminal act on the part of Marion Barry," he said. "It's a larger issue than that."

Barry said that he felt "fantastic" on what he described as the "160th day of Marion Barry being free of any mood-altering drug of any kind."

Earlier in the week, however, Barry's conduct surprised many, when he invited himself on stage during Nelson Mandela's address at the Convention Center and at times appeared to be rolling his eyes and contorting his face.

Also, shortly after a Virgin Islands woman testified at the trial Monday that Barry had forced her to have sex with him in 1988, the mayor turned up for an interview with Renee Poussaint on WJLA-TV (Channel 7) and perspired heavily while refusing to answer questions about the allegation.

On Wednesday evening, after testimony from Hazel Diane "Rasheeda" Moore that she and Barry used drugs at least 100 times, Barry and his wife turned up unannounced for the first of two rallies held by Farrakhan.

Barry, wearing a strip of African kente cloth around his neck, did not address the gathering of 10,000 at the rally. But he hugged Farrakhan in a display of unity on stage and smiled broadly when Farrakhan told the predominantly black audience that the mayor was "under attack" by the white establishment and urged Barry to seek reelection to a fourth term in office. The mayor announced recently that he will not run this year.

Stallings and Tawana Brawley, the former New Yorker whose allegation that she was kidnapped and raped by several white men was discounted by authorities, also joined Farrakhan.

Barry returned to the Convention Center for a second rally Thursday night, hours after the Vista sting tape had been released and broadcast throughout the world. This time he brought along his wife and their 10-year-old son, Christopher, who sat on stage.

Also attending was Savage, whose predominantly black district includes part of Chicago, where the Nation of Islam is headquartered. The Muslims have endorsed him for a fifth term.

Lurma Rackley, the mayor's press secretary, described Farrakhan's show of support for Barry as an act of friendship stemming from "a shared commitment to a national agenda."

Bruce Lehman, a lawyer and former Barry supporter, said the mayor is hurt and "it's nice to go someplace and have a lot of people admire you.

"He's looking for support in a very troubled period, and he'll take it wherever he can find it," Lehman said.Staff writer Matthew Lee contributed to this report.