WASHINGTON (AP) _ A former pharmaceutical wholesaler admits selling drugstores millions of dollars worth of drugs obtained from an illegal market that investigators say could supply consumers with unsafe or counterfeit medicines.

''Even though I knew deep inside there was something wrong ... I felt from a legal point of view I was probably OK,'' Marvin Sandler told a House subcommittee Thursday.

Sandler, who recently pleaded guilty to federal charges in connection with a drug diversion scheme, admitted that his dealings in the discount-priced drugs between 1981 and 1984 did not benefit consumers.

''I'd say the benefit to the consumer would be minimal,'' Sandler told members of the House Energy and Commerce oversight and investigations subcommittee. ''The benefit would be a lower price, and that is a maybe.''

Sandler said many of the diverted drugs he dealt in were obtained from suppliers who had bought them at discount prices by falsely telling manufacturers the medicines would be sold to overseas missionary or relief agencies.

Instead, the suppliers quickly shipped the drugs back to the United States, cleared by the U.S. Customs Service as ''American Goods Returned.''

Congressional investigators, presenting the results of their probe, told the panel the diverted drug market simply provided middlemen with high profits and exposed consumers to the dangers of ineffective or counterfeit drugs.

Improper handling and storage of drugs may render them ineffective by the time they are put on drug counters, they said.

''Chemical deterioration due to heat and poor storage would be the problem,'' David W. Nelson, a subcommittee investigator testified.

''The widespread trafficking in these drugs moving long distances around the world affords significant peril to American consumers,'' said Rep. John Dingell, R-Mich., the panel's chairman.

Dingell is pushing legislation to prohibit reimportation of American-made drugs by any firm other than the manufacturer. He contends this would curb a big source of the diverted drug market.

''My primary interest was in obtaining pharmaceuticals at the lowest possible cost so long as I was satisfied that there were no restrictions on sale on the package,'' said Sandler, who resigned as an executive of his family-owned Interstate Drug Exchange Inc. of Plainview, N.Y.

Sandler, whose guilty plea in Atlanta was part of an investigation by federal prosecutors that has obtained 80 convictions, said the major drugstore chains he sold to did not know the source of his supplies.

But these customers must have known from the discount prices they were paying that the drugs were from the diverted market, he told Rep. Fred J. Eckert, R-N.Y.

''In general, they didn't ask where, and I didn't tell them,'' Sandler said.

Sandler said suppliers sometimes asked him to return diverted drugs so they could be shipped overseas to satisfy the curiosity of the manufacturer. On one occasion, when he had already sold the drugs, Sandler said he placed generic tablets in the manufacturer's original packing case and sent them to be shipped.

On another, the supplier needed 11 pallets of liquid medicine to be shipped to a Middle East country. Sandler said he sent 2,000 one-gallon bottles filled with water at the suggestion of the supplier who noted ''it was going to a desert country.''

Nelson said drug reimports have been cut sharply since last September, when $2.5 million worth of ''American Goods Returned'' came back into the country. But he warned that once investigations by federal authorities come to an end, the traffic will increase unless legislation is enacted to curb the practice.

''Clearly the circuitous routes of these goods suggest the basic goal is to deceive U.S. manufacturers,'' Nelson said.

The volume of drugs shipped in recent years through Grand Cayman, a Caribbean island with a population of 10,000, ''would have kept that country awash in medicines for the rest of the century,'' Nelson said.