Mayor Marion Barry testified for more than four hours yesterday before a federal grand jury investigating allegations of financial misconduct by his former wife.

Afterwards, the mayor said he had appeared before the panel voluntarily and answered all questions asked. Barry said he was not and would not become a subject of the probe.

"I have been advised by the U.S. Attorney in a letter and verbally that I am not a target of the investigation," Barry told reporters as he left the federal courthouse. "I'm confident by my own knowledge," he added, "by what I've been involved in in the last 15 years in this town, that I won't be a target."

Barry would not discuss his testimony, but said he would cooperate with the panel, making additional appearances if asked.

However, Assistant U.S. Attorney William D. Pease, who is supervising the case along with Assistant U.S. Attorney Stephen R. Spivack, said there were no plans to call the mayor before the panel again.

The mayor was asked as he left if he thought the investigation involving his former wife would affect his anticipated campaign for relection next year. "No, why should it?" Barry responded.

For more than a year, the grand jury has been investigating published allegations that the mayor's former wife, Mary Treadwell, and two others stole, diverted or misappropriated more than $600,000 from P.I. Properties Inc., a real estate spinoff of the now defunct self-help group, Youth Pride Inc., between 1974 and 1978.

Treadwell has denied any wrongdoing. Barry was not named in any of the allegations. Treadwell and Barry were separated in 1976 and divorced in 1977. He has said he severed all relations with Pride in early 1978, before he was elected mayor.

Barry, who founded Youth Pride with Treadwell in 1967 and used it to help launch his career in local politics, entered the grand jury room shortly after 2 p.m. yesterday carrying a single manila envelope. His legal counsel, Herbert O. Reid Sr., waited outside the room.

While the specifics of Barry's grand jury testimony are secret, Reid said earlier in the afternoon that he presumed Barry would be asked for a detailed accounting of his knowledge of his former wife's finances, any intermingling of their funds and Barry's roles as an officer in Youth Pride and its spinoff organizations.

It has long been expected that Barry would be one of the final witnesses to appear before the grand jury, and his appearance yesterday seemed to be a clear signal that the long-standing investigation is drawing to a close.

Six months ago, Treadwell made a brief, routine appearance before the same grand jury to certify that she was the official custodian of records of nearly a dozen Pride-related organizations that have been included in the federal investigation. Treadwell also gave federal prosecutors examples of her handwriting.

Prosectors have not demanded further grand jury testimony from Treadwell. Like any other witness before a grand jury, Treadwell could decline to answer substantive questions about her activities, claiming the Constitution's Fifth Amendment privilege against self incrimination.

Barry said yesterday that he never invoked his Fifth Amendment privilege during his testimony. Reporters asked Reid if Barry had been offered or given immunity from prosecution. Reid said that no such discussions had taken place.

Youth Pride Inc., founded as a self-help organization in the final years of the civil rights movement, shut its doors permanently in August. Treadwell has blamed the demise of the organization on a series of articles published in The Washington Post in October 1979 that detailed the allegations.

P.I. Properties operated the Clifton Terrace Apartments, a once grand but now run down 285-unit complex at 14th and Clifton streets NW, which has been the principal focus of the federal investigation. Treadwell has insisted that P.I. Properties had no relation to Youth Pride Inc.

Barry has told reporters that Treadwell told him in 1974 that she was interested in buying Clifton Terrace. Barry said, however, that he told her that he did not intend to be around to help her with the project because of his plans to run for City Council.

Barry has said that he and Treadwell always filed separate income tax returns.