The Justice Department yesterday formally apologized, and agreed to pay $25,000 to a former official who became the target of a lengthy investigation into allegations that he and his wife had passed classified material to South Africa.

Assistant Attorney General Stuart M. Gerson also acknowledged that the Office of Professional Responsibility (OPR), the Justice Department's internal watchdog unit, had erroneously stated in its annual report that the official, Roger Pilon, had resigned because of evidence developed during the espionage probe.

The unusual monetary settlement, reached under the threat of a lawsuit from Pilon's lawyers, also brought to light new evidence about bitter internal wrangling within the Justice Department that led to the resignation of deputy attorney general Donald B. Ayer two months ago.

After intially writing a letter to Pilon's lawyers in March conceding that the OPR report had been in error, Ayer later wrote another letter in April absolving OPR director Michael E. Shaheen Jr. of any responsibiity for the mistake.

That letter, written on April 30, infuriated Attorney General Dick Thornburgh and his senior staff, precipitating a confrontation that resulted in Ayer and his principal deputy, Peter Nowinski, leaving the Justice Department on May 10, department officials said. Ayer declined to comment yesterday.

"Naturally, I'm pleased that the record is finally set straight," said Pilon, the former head of Office of Asylum Policy and Review, who is now a senior fellow with the Cato Institute. "This has been a two-and-half-year-long Kafkaesque nightmare."

Pilon, who was policy director of the State Department's Bureau of Human Rights before joining Justice in May 1987, was accused by FBI agents in early 1988 of passing a classified document to his wife, who in turn allegedly gave it to South African officials. The evidence against the couple, which led to the revocation of Pilon's security clearance, came in part from National Security Agency electronic intercepts of South Africans diplomats, officials familiar with the matter confirmed.

But Pilon said yesterday the entire matter was a misunderstanding. His wife, Juliana Pilon, who at the time was a Heritage Foundation analyst, did have a telephone conversation with a South African press officer in which they discussed exchanging documents, but not the classified State Department report about Soviet activities in South Africa that investigators believed had come from Pilon.

Then-assistant attorney general Dee Benson wrote a letter to Pilon clearing him of wrongdoing in September 1988. But in an annual report released last November, OPR stated that an espionage investigation into an unidentified official, later identified as Pilon, had developed "sufficient cause" to terminate his political appointment and that the official "resigned prior to the initiation of removal proceedings."

Gerson in his letter conceded that both of these statements were wrong, that Thornburgh "concurred" in the finding, and that Pilon's identity was improperly disclosed to the press. "The department . . . apologizes for any suffering or inconvenience that the department caused to Mr. Pilon and to Mrs. Pilon," Gerson stated.

Department sources said yesterday that OPR had not been informed of Benson's letter exonerating Pilon when it wrote its report.