Gordon Smith, a penniless drifter at 37, spent nine months in the criminal ward of a state mental hospital here accused of being the "stocking mask rapist" who assaulted five women and terrorized this small Virginia town.

At the same time, Theodore Gray, 23, spent 71 days locked up in the Augusta County jail, also accused by Staunton police of committing one of the "stocking mask" rapes.

Now, Augusta prosecutor Raymond C. Robertson says he is convinced that a third man being held by local police is the one who last year forced his way into the homes of five Staunton women, held a knife to their throats and raped them. robertson says he is "sorry" that Gray and Smith were held so long for crimes they did not commit.

"We had positive identification by the victims in both cases, so I don't have any problems with the way police handled things," Robertson said yesterday. "It's just a damned shame they were wrong."

Many local citizens, however, say the shame is that Smith and Gray fell victim to a racially tinged miscarriage of justice in this hilly Virginia city located in the shadow of the Blue Ridge about 150 miles southwest of Washington. All five women in the case, including a student at all-female Mary Baldwin College, are white; Smith and Gray are black.

"Gordon was a scapegoat," says Mona Allen, who runs the Valley Rescue Mission where Smith has stayed since his release from Southwestern State Hospital in Marion last Saturday. "The police got him because the public was putting pressure on the police to arrest people."

Smith was arrested April 11, 1979, and charged with one of the five rapes. Gray was arrested in September, while Smith was in custody, and charged with a separate rape count. He was set free in November.

Staunton police even consulted two self-described psychics as they struggled to unravel the case and calm the community's fears.

Today, Smith keeps everything he owns in a slim metal locker at the Valley Rescue Mission. Accustomed to wandering from place to place since he left a Kentucky orphanage at the age of 12, Smith says he works odd jobs to stay alive and often sleeps on park benches.

Gary came back from his lengthy stay in jail to find he had lost his old job as a laborer because his female coworkers were afraid to be alone with him.

Robertson has called on local official to compensate Gray for his time in jail and the loss of his job. But members of Staunton's black community, which is about 10 percent of the town's 21,300 population, say money can't erase what many see as a crime of racism.

"The whole incident caused a needless repolarization of attitudes along cultural and subcultural lines," says the Rev. James H. Scott, a local school administrator and an officer of the Virginia NAACP. "When something like this happens, each individual feels potentially threatened."

Scott says the NAACP is considering suing the Staunton Police Department on Gray's behalf and charges that police were aware when they seized him that Gray did not fit the descripton of the rapist.

Few people in this quiet town, where talk of the rapist has buzzed incessantly on porch steps and in coffee shops since the rapes began last March, seem to doubt that police have their man in James B. Robinson, the truck driver who police say confessed last week to all five rapes.

Local authorities say the 22-year-old Robinson, who was arrested on a routine breaking and entering charge, told them about details of the rapes that only the rapist could know. He then led them to the mask described by each of the victims, a cap-like affair devised from a woman's stocking that covered half of the assailant's face, officers said.

Police said Robinson matched the description given to them by two psychics, who had offered their services during the height of the town's panic last fall. At the time, women's groups were approaching the City Council and demanding action, police were working long hours of overtime on the investigation, and civilian groups wre helping police patrol the streets in search of the assailant.

"We were welcoming any assistance from anybody at that time," said Sgt. Lacy King, one of two officers who directed the rape investigation. "I have to admit that the first thought of it [using psychics] to me was weird, but when you're doing police work you've got to do a lot of things that are weird. You can't take anything for granted."

King said self-described psychic Noreen Renier, who teaches extrasensory perception at the University of Virginia, told police the rapist had a scar on one arm, made deliveries of some sort, and "went round and round." A male psychic, who said he did not wish to be named, told police the assailant lived in a brick building at the base of a hill.

According to police, Robinson has a scar, lived in a brick house at the bottom of one of Staunton's many hills, and drove a cement delivery truck that rotated constantly in a circular motion.

Robinson is being held in Augusta County jail without bond. He is scheduled to appear in Augusta County Circuit Court next month.

Gray, who said he lost his job as a laborer for the U.S. Forest Service when colleagues refused to work with him, says he is bitter about what he sees as racially motivated harassment by police.

"The composite they put out on the rapist said he was clean-shaven, light-skinned, and weighed about 160 pounds," says Gray, who wears a mustache, beard and sideburns, has dark skin and weighs 140. "There was no way they could have thought it was me."

Joyce Pendleton, Gray's mother, says there was a feeling in the black community on Sept. 16, the day here son was taken into custody, that police would make an arrest to quell the public's uproar.

"I remember coming out of the Negro Baptist church, and people were saying "they're going to pick somebody up this day,'" she says. "Some of the black men had gotten so they were afraid to go on the streets because they didn't know what the police would do."

Proscutor Robertson labeled as "barbaric and medieval" the charges by Gray and his mother. He said Gray had been arrested after a rape victim had indentified him as her assailant and that he subsequently flunked a lie detector test.

It was not until after another rape had been committed while both Gray and Smith were being held that local officials began to think that "we've arrested the wrong guy," Robertson said.

Shortly afterward, in late November, Robertson declined to prosecute Gray and the young man was set free.

Smith, however, was not released until after the third suspect was taken into custody last week. Charges against Smith are scheduled to be dropped Wednesday.

Robertson said psychlogists at the hospital determined that Smith was incapable of caring for himself, so he and Smith's attorney, Public Defender William E. Bobbitt, decided not to rush in trying to get Smith released. "He was obviously getting needed attention there," Robertson said.

Allen, director of the shelter to which Smith returned after his stay in the hospital, conceded that Smith had some "mental problems" but said that was no reason to lock him up for a crime he did not commit.

"We've never had any trouble with him," Allen said. "They just locked him up because he was passing through. "They figured he was apt to be looking for trouble."

According to law enforcement officials, the student had identified Smith as her assailant when she saw him standing on the street.

Inside the mission, Gordon Smith sat placidly today, planning to get a job as a dishwasher and save some money to go to El Centro, Calif.; and pick lettuce.

He said he was confident that he would get out of the state hospital because he had committed no crime. "I was sure that if I went to court, whoever it was who said I had raped them would see it wasn't me."