A Central Intelligence Agency officer spent last week in a Northern Virginia jail before finally convincing prosecutors that he was not the man they wanted as a key figure in a huge West Texas marijuana smuggling ring.

What happened to the CIA's William R. Hartley, 30, emerged only yesterday, a day after prosecutors quietly dismissed the charges against him in an Alexandria court.

It appeared to have been a classic case of mistaken identity, Drug Enforcement Administration officials said yesterday. But no one connected with the case, including the CIA, could say for certain how Hartley, arrested March 6 at the agency's Langley headquarters, was drawn into the bizarre affair.

How could such a mistake happen?

Hartley apparently has given two versions to authorities. The most ironic -- which sources said he told DEA -- is that he somehow lost an identification card that was used by the real drug smuggler to create a fictional identity.

But Hartley told his CIA superiors that he had not lost any of his identification cards, and CIA officials suggested there is "another Bill Hartley" who just happens to look like -- and therefore was mistakenly identified as -- The CIA officer.

The episode is complicated, sources said, because at least four witnesses identified pictures of the CIA's Hartley as the person who bought gasoline and rented motel rooms in Texas last fall while under surveillance by state and federal narcotics investigators.

However, a Carlsbad, N.M., police lieutenant told federal authorities in Alexandria Tuesday that the CIA officer was not the man he had seen pilot a plane to Mexico on an alleged marijuana smuggling trip.

And sources said that on two of three key dates last fall when the drugs suspect "Hartley" was seen in Texas, the CIA's Hartley was attending a "training course" in Warrenton, Va., 45 miles west of Washington.

"We think now it's just a doozy of a case of mistaken identity," one official close to the case said yesterday.

Dale Peterson, a CIA spokesman, confirmed only that Hartley was an agency employe. He insisted that the CIA does not pose its officers as drug smugglers, even for undercover operation.

Hartley's troubles began Oct. 20 when a man identified as him was spotted with known drug traffickers in Carlsbad. During the first week of November the same person was observed near a plane unloading marijuana near Austin.

On Nov. 28, the man was again sighted in Carlsbad getting into a plane that a U.S. Customs Service plane followed into Mexico. The first plane was found abandoned the next day near Waco, Tex., with traces of marijuana in it.

Law enforcement officials here and in Texas pieced together this account of how the CIA's Hartley was identified and arrested.

Officers watching the drug ring were told by gas station attendants and motel clerks in Carlsbad and Waco that a "Bill Hartley" had signed gasoline tickets and room registers.

Believing the suspect to be a pilot, authorities checked the Federal Aviation Administration records and found that William R. Hartley had a student pilot's license and a Dallas address.

A check of Dallas city records uncovered a driver's license photo, which the withnesses picked from a group of pictures as being the "Bill Hartley" they had seen.

An arrest warrant for "William R. Hartley" was authorized Feb. 14 by Jamie Boyd, the U.S. attorney in San Antonio who is coordinating the extensive drug investigation in West Taxas. Boyd could not be reached yesterday for comment about the apparent mix-up in Hartleys.

DEA agents who visited Hartley's Dallas address found that he had moved to Virginia recently, and a check of his address here led to the discovery that he was a new CIA employe, the sources said.

DEA then told the CIA security office about Hartley and the arrest warrant. On Tuesday, March 6, he was turned over to DEA agents at CIA headquarters.

He spent a week in jail, sources said, apparently because the CIA felt it could not vouch immediately for the honesty of its new employe. A check of his background showed that he did have a student pilot's license, but CIA spokesman Peterson said Hartley is not a pilot for the agency.

While Hartley spent time in the Fairfax County jail, the CIA did check the dates and found that he was in Warrenton, not smuggling drugs in Texas, sources said.

Another factor made authorities wonder if they had the wrong man. They doubted the CIA security check for a new employe would miss such obvious signs that he was involved in smuggling marijuana.

The case was dropped quietly Tuesday afternoon in a short hearing before U.S. Magistrate W. Harris Grimsley in Alexandria. Assistant U.S. Attorney Robert Firth said the government was dismissing because it could not prove after all that Hartley had been in Texas under surveillance.

There was no indication in the public court record that Haryley works for the CIA. His attorney declined to comment at all on the case. "It's a sensitive situation because of his (Hartley's) situation, employmentwise," he said.