Sen. Charles E. Grassley (R-Iowa) accused the Reagan administration yesterday of "conspiring with foreign governments" by considering the wishes of South Africa and the Soviet Union in deciding whether to prosecute protesters arrested in demonstrations at their embassies here.

Grassley, questioning U.S. Attorney Joseph E. diGenova at a Senate Judiciary subcommittee hearing on a D.C. law that prohibits three or more persons from congregating within 500 feet of any embassy, said he found it "incredible" that the "policies of these countries take precedence over our own freedoms of expression."

Enforcement of the 48-year-old law has become controversial in the last 18 months because hundreds of persons arrested outside the South African Embassy during apartheid protests have not been prosecuted, while demonstrators at the Soviet Embassy protesting treatment of Soviet Jews have been.

DiGenova and State Department legal counsel Abraham Sofaer testified that enforcement of the law, based partly on whether the governments of the affected embassies favor prosecutions, is necessary to ensure that other countries afford proper protection to U.S. embassies and diplomats overseas.

Sofaer added that considering the requests of foreign countries is a proper foreign policy concern and one that Congress recognized when it enacted the law. He said any changes in the law, including Grassley's suggestion of possibly reducing the legal distance for demonstrations to 100 feet, should be taken up by Congress.

"I am completely convinced that the statute is being enforced in a fair . . . and humane manner . . . by the U.S. attorney, if not by other law enforcement officials," Sofaer said.

But diGenova blasted former D.C. corporation counsel Inez Reid for what he called her "unconstitutional" decision not to prosecute protesters for crossing police lines -- a D.C. law over which the U.S. attorney has no control -- outside the South African Embassy "because she agreed with the content of their speech and disagreed with the policies of South Africa."

"This office has never based its exercise of prosecutorial discretion in any case on such constitutionally impermissible reasons," diGenova said.

But several persons who have been arrested outside the Soviet Embassy, including Rabbi David Oller, chairman of the Washington Board of Rabbis' Soviet Jewry Committee, said they were singled out for prosecution.

"While protesters at other embassies were not prosecuted," Oller said, "the U.S. attorney chose to prosecute us, a clear instance of selective prosecution."