BERLIN (AP) _ East German authorities foiled a plot to hijack a U.S. airliner in West Berlin by two people carrying diplomatic passports from a Middle East country, sources in the West Berlin Senate said Saturday.

The sources said security agents in communist East Germany, acting on a tip, arrested the two ''about four weeks ago'' at East Berlin's Schoenefeld Airport and found explosives in their bags.

The West Berlin newspaper Tagesspiegel said the two were deported ''out of consideration'' for East Germany's relations with the unidentified Middle East country.

The arrests coincided roughly with the June 14 hijacking of a TWA flight from Athens, Greece, said the sources. They said the two planned to hijack an American plane in support of Shiite Moslem extremists who seized the TWA plane.

The TWA hijackers killed a U.S. Navy diver aboard the Athens-to-Rome flight. Thirty-nine other American passengers and crew were held hostage for 17 days in Beirut. The hijackers were widely reported to belong to the militant Hezbollah, or Party of God, organization.

The more moderate Shiite Amal militia took custody of most of the hostages and helped negotiate their release.

Pan American World Airways is the only American airline that flies to West Berlin, a Western enclave 110 miles inside East Germany. West Berlin, governed by the West Berlin Senate, has been under the administration of the United States, Britain and France since the end of World War II.

The senate sources spoke on condition they were not to be identified.

Soviet officials informed the Western allies in Berlin of the arrests, but could give no other details and did not say from which country the two came. East Germany is a close ally of the Soviet Union.

Thomas A. Homan, a spokesman for the U.S. mission in West Berlin, said he had no comment on the reports.

''But I don't want to deny it,'' he told The Associated Press.

The Bonn-published Die Welt newspaper said Saturday the arrests were the first result of an agreement between the United States and the Soviet Union to fight terrorism.

Quoting unidentified intelligence sources, Die Welt said those arrested apparently acted as couriers for explosives and probably planned to meet accomplices in West Berlin.

A State Department source in Washington, speaking on condition of anonymity, said he was aware of the reported arrests but could not comment because it involved an intelligence matter.

Asked if the report indicated U.S.-Soviet cooperation to combat terrorism, the source said: ''You know that we have talked to the Soviets and East Germans about this, and if it is true, we would be very pleased. But for now we have no comment.''