Mehmet Ali Agca, the Turkish gunman who shot Pope John Paul II, said today he had been forced to invent testimony during the course of his pretrial interrogation because of slanders against him by the Soviet Union and Bulgaria.

The contradictions and other "inventions" in Agca's testimony in his second trial here, compared to his pretrial testimony, have become an important element in the courtroom. At one point today, Judge Severino Santiapichi asked: "How can I understand when you are telling the truth or not?" Both the judge and the prosecutor, who must depend on Agca's testimony against Bulgarian and Turkish codefendants, asked the key witness if he was going to continue to invent testimony.

On trial with Agca are three Bulgarians and four other Turks who are accused of acting as his accomplices.

In court today, Agca also denied allegations that he had been pressured by the Italian Mafia into implicating the Soviet Bloc in the assassination attempt.

The claim of a "Mafia connection" to Agca's confessions was made in a magazine interview by a former mobster, Giovanni Pandico, who has turned state's evidence in a trial in Naples of suspected gangsters. Pandico told journalists yesterday that the Camorra, as the Neapolitan branch of the Mafia is known, was acting on behalf of a general in the Italian secret service who is now on trial for abuse of office.

Pandico's allegations could be tested in any one of three overlapping court cases now under way in Italy: the Agca trial, the trial of Camorra suspects and the trial of former members of the military branch of the Italian secret service. A so far tenuous link between the three cases is provided by the fact that Agca was held in the same prison as Camorra members, who are in turn alleged by Italian prosecutors to have had dealings with former officials of the secret service.

Soviet Bloc propagandists and leftist Italian newspapers have claimed for some time that Agca was "fed" in prison with details on the Bulgarians he later accused of being his accomplices but that have failed to provide convincing evidence to support the assertions.

Giving evidence today on the eighth day in the witness stand, Agca was pressed by Judge Santiapichi to explain lies and contradictions in his pretrial testimony. He replied by citing Pandico's allegations as an example of the kind of pressures to which he was being subjected.

"The Bulgarians publish false information, the Soviets too. Every day some false news comes out and therefore I am forced to invent things as a way of responding. To inventions, I reply with an invention," Agca said.

The judge asked Agca to explain why he had initially identified an apparent accomplice photographed running away from St. Peter's Square on the day of the May 1981 assassination attempt as Kolev, the alleged code name for Todor S. Aivazov, a Bulgarian defendant. After the Italian media reported that the heavily built Aivazov looked nothing like the slim figure in the picture, Agca changed the identification to a Turkish accomplice, Oral Celik.

"How can I understand when you are telling the truth or not?" Santiapichi asked. "Why did you say it was someone else? I am trying to understand your inventions."

Agca replied by saying he had wanted to protect Celik, a childhood friend, and to respond to the Bulgarians who were slandering him.

After both the judge and the public prosecutor asked Agca if he was continuing to invent testimony in court, the Turk insisted that he was not.

As a defendant in the present trial, Agca is not required to give evidence under oath and cannot be accused of false testimony. After being sentenced to life imprisonment in July 1981 for attempting to murder the pope, he is now being tried on the lesser charge of smuggling a weapon into Italy.

The papal assailant was also questioned closely today about his claim that his Bulgarian accomplices provided a diplomatically sealed truck to allow him and Celik to escape from Rome after the assassination attempt. Agca's credibility during the pretrial investigation was boosted when it was discovered that such a truck had left the embassy shortly after the shooting in St. Peter's Square.

Asked how he and Celik could have been smuggled aboard the truck after it had already been sealed by Italian customs officials, Agca replied: "The Bulgarians are masters at falsifying things."