It involved a two-week diplomatic tug of war over the terms and timing for release of more than 700 Lebanese prisoners in Israel, and at times it seemed to be causing severe strains in the historically close alliance between the United States and Israel.

But in the end, the two countries patched together an "understanding" that was the key to unlocking the doors to freedom for the 39 Americans held in Beirut.

Yet, even after the American hostages left Beirut yesterday, the implications of this understanding remained so sensitive that neither government was publicly willing to acknowledge its existence, or to speculate about the long-term effects that the stresses and suspicions generated by the hostage episode might have on U.S.-Israeli relations.

Within hours after Arab gunmen hijacked TWA Flight 847 out of Athens on June 14 and demanded the release of Israel's mostly Shiite Moslem prisoners, the core issue was clear. The Reagan administration wanted Israel to be ready to release its prisoners without being asked by the United States, but Israeli Prime Minister Shimon Peres' government said it would act only in response to a specific and public U.S. request.

The difficulties in bridging the gap between these positions were compounded by two factors: an apparent lack of high-level communication between the two governments during the early days of the hijacking episode, and Israel's feeling that the administration, in the words of Defense Minister Yitzhak Rabin, "played games" by insisting publicly that Israel stand fast while simultaneously sending signals that it should let its prisoners go.

Israeli officials refuse to discuss their differences with Washington. But they and their supporters in this country are understood to believe that Secretary of State George P. Shultz and national security affairs adviser Robert C. McFarlane encouraged them in unmistakable terms to hew to a tough line, while other White House officials used oblique statements and press leaks to prod them toward releasing the Lebanese.

Sources who advance this analysis say they do not know whether those conflicting messages represented a deliberate, two-track U.S. strategy or reflected a split within the administration about how to deal with Israel.

These sources say the Israelis are inclined to the view that there were two camps, and that the one headed by Shultz and McFarlane ultimately prevailed in persuading the administration to agree that Israel should not release its prisoners until the Americans were safely out of captivity.

On the other side, U.S. officials were clearly irritated by Israel's initial tendency to characterize the situation as a purely American problem and to publicize its position that it would release the Lebanese only if the United States publicly asked it to do so.

Both sides were prompted by a desire not to be viewed by domestic constituencies or foreign adversaries as bowing to terrorist demands. The result was the bizarre spectacle of the two longtime allies standing like two kids on the edge of a diving board, each trying to nudge the other into jumping first into a diplomatic pool labeled "linkage."

The United States insisted throughout that it was not asking or pressuring Israel to link the release of its prisoners to the freeing of the American hostages. But at several important junctures U.S. officials did make pointed remarks signalling their belief that the detainees in Israel should be sent home.

On June 17 White House spokesman Larry Speakes publicly recalled an April 3 U.S. statement that Israel's detention of the prisoners violated international law. And, last Tuesday, after White House officials privately leaked word that they had abandoned efforts to pressure Israel, Vice President Bush appeared to take the administration through another about-face when he told a news conference in Bonn, "We think that people being held against international law should be released."

These crosscurrents fanned Jerusalem's feeling that the United States was trying to make Israel appear "the bad guy." Peres responded with a phone call to Shultz on June 21 and a decision on June 23 to release 31 of the Lebanese prisoners, and the dispatch of a letter to Reagan June 24th pledging Israel's cooperation in whatever needed to be done to secure the hostages' freedom.

Although the extent and sequence of subsequent U.S.-Israeli dealings is not known, Peres' initiative is believed to have opened a new high-level dialogue, apparently conducted in part by McFarlane and Israeli Ambassador Meir Rosenne. It resulted in the "understanding" that Israel's prisoners would remain in detention until the hostages were freed and then would be released according to what is expected to be a staggered but rapid timetable.

That understanding, which finessed the question of who should take the initiative to release the Lebanese prisoners, reportedly was largely agreed upon by last Thursday.

But on that same day, in another of the confusing twists that have characterized relations between the two countries throughout the hostage impasse, a well-connected White House official told The Washington Post that Israel should release its prisoners without being asked; and on Friday, other White House officials were denying to American-Jewish organizations that his remark reflected U.S. policy.

The question now is whether the two countries will be able to follow the pattern of past U.S.-Israeli disputes and put these differences behind them, or whether this latest incident will leave a residue of mistrust that could affect America's longer-range policy goals in the Middle East.