The United States and Britain played a major role in paving the way for West Germany's spectacular rescue of the 86 Lufthansa hostages, diplomatic sources disclosed yesterday.

President Carter sent a private message to the leftist Somali president, Mohammed Siad Barre, in the hours before West German Commandos stormed the hijacked aircraft at Mogadishu airport, urging him to assist the Bonn government in any way he could.

Britain joined the United States in bringing diplomatic pressure on Somalia, and authoritative sources said two members of Britain's elite Special Air Service commando force flew into Mogadishu and advised the crack German unit that freed the hostages.

While the White House declined to disclose details of Carter's secret massage to Siad Barre, the President yesterday sent a second cable to the Somali leader publicly expressing "my personal appreciation for your vital and decisive role" for the safe release of the hijack victims.

The President's national security adviser, Zbigniew Brzezinski, also declared that the "United States would be prepared to take whatever action it had to, or was capable of " in the event of a future hijacking of a U.S. airliner.

While Brzezinski refused to discuss details of U.S. preparations for such a contingency, the Pentagon revealed yesterday that a special force of American troops and equipment has been assembled and trained to cope with terrorist incidents.

The Pentagon said the U.S. force, drawn from all three branches of the armed services, has conducted training exercises. A Defense Department spokesman declined to answer questions, however, about how many men have been trained or where anti-terrorist units are deployed.

President Carter's personal appeal to Somali President Siad Barre Monday was hand-carried to the Somali leader by the U.S. ambassador in Mogadishu, John L. Loughran.

Like several other earlier U.S. messages to the Somali government, the President's cable did not specifically ask Siad Barre to permit West German commandos to attempt to rescue the hostages, since the U.S. insists that Bonn did not tell Washington it had decided on this step until the 86 victims were actually free.

"We were aware that they had contingency plans. We know this was one of the possibilities they were considering," a high ranking U.S. official said. "But the Germans understood this was their responsibility and they knew they had they resolve it."

For Somalia, a socialist state, the presence of the hijacked plane presented the anomaly of a leftist-oriented government cooperation with major Western powers to overcome leftist terrorists.

At the same time, the Somalis have been having their differences with their once sole patron and arms supplier, the Soviet Union, and have been courted by conservative Arab governments who have supported Somalia's confrontation with Ethiopia's over the country's Ogaden region.

While the United States was urging the Somalis in general terms to cooperate with West Germans, the British were aiding the Bonn government's West Germans rescue efforts in a much more direct way.

Two British commandos were dispatched to the desert airport at Dubai late last week after West German Chancellor Helmut Schmidt appealed to British Prime Minister James Callaghan for aid.

With a powerful telephoto lens, the British commandos-one, major: the other, an enlisted man-took highly detailed photographs of the Lufthansa jetliner and the hijackers. The two men also were with the German commandos in Mogadishu, sources siad, but did not take part in the actual assault on the plane.

Diplomatic sources said the British also joined the United States and Britain in ending the Luthansa saga was evident yesterday in the warm thanks Schmidt extended to both Callaghan and Carter.

In a "Dear Jimmy" cable to the President, Schmidt said: "I know and appreciate that you personally and your government did everything to assist in a happy solution of the ordeal."

When Callaghan arrived in Bonn later in the day for previously scheduled talks with Schmidt, the West German chancellor said that the "calm and trustful cooperation" of the British had "strengthened the bonds of friendship between our two countries."

Significantly, Schmidt had no similar message of thanks to France, which the day before had expressed its "full support" for West Germany in whatever steps it found necessary. Sources suggested that the French had tried to maintain a low profile in the affair out of concern for the reaction of Arab and African states, and had played no part in the effort to influence Somalia.

Even so, French President Valery Giscard d'Estaing was one of the first world leaders to congratulate Schmidt on the successful freeing of the hostages. Giscard termed the action "not inhuman methods but a victory for democracy for all free men."

The Isralie government, the world's leading advocate of tough treatment for airline hijackers, also was quick to commend Bonn. Prime Minister Menahem Begin cabled: "It was indeed a salvation in which all free men rejoice."

The International Federation of Airline Pilots Associations, however, called on pilots around the world to begin a 48-hour strike next Tuesday to protest the hijackers' killing of the captain of the Lufthansa jet, and to back demands for action against air piracy.

Pilots form Britain, Sweden, Norway, Denmark, Greece and Belgium quickly voted to join the strike, but there were indications the walkout might be called off if the United Nations General Assembly agrees to act on three conventions now before it outlawing hijacking.

We are urging our State Department and the President to get the United Nations to hold a General Assembly meeting to discuss the anti-hijacking convention," a spokesman for the U.S. Air Line Pilots Association, which has not yet decided to join the strike, said.

While the Pentagon refused to provide any indication yesterday of the size of the special armed services team that has been trained to deal with the hijacking of U.S. aircraft overseas, the Federal Bureau of Investigation said it has trained 900 to 1,000 agents to deal with domestic terrorist incidents.

A number of additional details also emerged yesterday about the contracts that took place in the week following the Luthansa hijacking between Bonn and Washington.

When it was learned shortly following the hijacking that two Americans, Mrs. Christine Santiago, and her son, Leo, 5, of Sandee, Calif., were aboard the plane, the State Department set up a special Luthansa Hijacking Task Force that went on round-the-clock duty in the Operations Center.

A similar "Crisis Center" was set up by the West German government in Bonn, and the two governments quickly began exchanging whatever information they had been able to gather about hijacking.

"We had messages in and out of here like you'be never seen," one participant said yesterday.

The West German government advised the U.S. that if it saw ways to be of assistance, this would be appreciated. "And we did communicate with friendly governments, urging them to support any German request for assistance," a diplomatic source said.

Officials said, however, that the United States did not at any time loan either anti-terror experts or special equipment to the Germans, and how to proceed. "That's their responsibility," a high-ranking source said.

When the week-long saga came to an end yesterday on the airfield in Mogadishu, the United States received first word of the Commando raid in a phone call from the U.S. Embassy in Bonn.

Minutes later Chancellor Schmidt's office was on the telephone to the White House, anxious to relate the glad tidings to President Carter.

Washington Post correspondent Bernard Nossiter in London contributed to this story.