On the last afternoon of their talks in Leeds Castle, the foreign ministers of Egypt and Israel had their first lengthy discussion ever of the ultimate problem of a Middle East peace settlement - the future of the holy city of Jerusalem. Surprisingly, both sides came away mildly encouraged.

The exchange developed out of a remark made in passing by Israeli Foreign Minister Moshe Dayan on Tuesday, the first day of talks that Jerusalem should not be a major problem. The intrigued Egyptians wanted to know more, and so almost an hour of talk on Wednesday afternoon was devoted to the unscheduled topic.

According to participants, the Egyptians made it clear that they do not envisage a redivision of the city into the sealed and hostile zones which prevaled under Jordanian rule of East Jerusalem from Israel's independence in 1948 until the 1967 Arab-Israeli war. Jerusalem should be one city with free and open access and a single set of city services, Dayan was told.

The Egyptian side insisted that Arab sovereignty and that Arab policemen rather than Israeli forces should patrol the area, which was annexed by Israel shortly after the 1967 war. But the Egyptians said the practical arrangements of the zones should be similar to those of metropolitan Washington, where many thousands of commuters and tourists travel back and forth daily between the District of Columbia, Maryland and Virginia with hardly a thought about the change in jurisdiction.

Dayan, before leaving London for Jerusalem - Israel's capital and a holy city for Jews, Christians and Moslems alike - said that "my impression is that in terms of daily life, we are not so far apart on the question of Jerusalem." He conceded, however, that the difficult problem of sovereignty remains, unresolved. An Egyptian official said that his side was also encouraged by the Jerusalem exchange. Both sides seem to consider this bedrock problem, often considered the most difficult of all Middle East issues, to be negotiable.

This and other points of surprisingly common perspective - including agreed methods for controlling terrorism on the West Bank and Gaza Strip, and agreement on the necessity for free exchange and travel between those areas and their Arab and Israeli neighbors - and bright spots in an often dark and sometimes forebording picture resulting from the two days of U.S.-sponsored talks.

For every item of unexpected consensus illuminated in the detailed Egyptian-Israeli talks, several items of inrelieved and seemingly intractable conflict were etched more clearly than ever before in the arguments of the negotiators. Among these was the question of Israeli withdrawal from the West Bank, the most basic issue standing in the way of comprehensive Middle East settlement sought by the Carter administration.

Egypt, with U.S. encouragement, sought to assure Israel that, in return for an agreement to withdraw, its security would be considered and protected in arrangments for transition and and eventual successor regime in the West Bank and Gaza Strip. To this and, according to participants in the talks, Egyptian officials took the lead in discussion of security plans lasting nearly two hours Tuesday afternoon.

Those discussed included demilitarized zones, of limited armament, early warning stations and the positioning of United States or other neutral troops in crucial areas along the Israeli border. When Israel balked at U.N. forces, due to lack of confidence in the international body, the Eqyptians are reported to have suggested that they would accept any other task force of neutral nations.

In this discussion, Israel was told that is military garrisons could remain in the West Bank during the proposed five-year transitional period. Egyptian sources said this position was taken with the reluctant approval of a number of moderate Palestinian leaders recently consulted by Cairo.

Dayan, however, reportedly said in the private diplomatic session as he has in public , that no conceivable security guarantees or arrangments could substitute for continued Israeli control of territory from which threats could be launched. In keeping with the established position of the Begin government he refused to contemplate Israeli withdrawal from the West Bank.

Dayan repeated after the Leeds talks that Israel "absolutely rejects" any commitment to the principle of withdrawal until a much later stage.

Israeli and Eqyptian officials agree that the two sides are very far apart on the issues of security and withdrawal. This is an area which Secretary of State Cyrus R. Vance has singled out for special U.S. interest. By suggesting that defense ministers and military experts be brought to the next round of talks, Vance has hinted that it will a subject of concentration in the future.

Neither Vance nor the Egyptians are under any illusion that a major shift in the position of the Begin government on withdrawal is likely during the two weeks before the next Vance mission to the Middle East. Disagreement over the priciple of withdrawal from the West Bank was at the heart of the thunderous dispute between Prime Minister Menachem Begin and President Carter early this year, and Begin did not budge.

Asked about possible shifts in the Israeli position, Dayan said yesterday that unless both sides make some changes "there will never be an agreement."

Vance is hopeful but by no means confident that with the differences as well as points of agreement better understood between them both sides will take some conciliatory decisions before he returns to the area in early August.

Egyptian President Anwar Sadat has been saying that further talks are useless unless there is a "new element" in the Israeli position. There were some hints at Leeds Castle that Egypt is asking for a public gesture of good intentions by Israel such as a voluntary withdrawal of some forces in the Sinai. The Israelisseemed interested according to participants in the meeting, but wanted to know what Sadat might offer in return.

Vance's plan is to go first to Israel, then to Cairo, and then to a yet-to-be-chosen meeting spot for himself and the two sides. His trip is scheduled to begin early next month and last from a week to 10 days.

The next Vance mission to the Middle East will be his fifth in the 18 months of the Carter administration and, in some respects, his riskiest and most important. If he can build on the Leeds talks to encourage flexibility and reestablish fullscale Egyptian-Israeli negotiations, the likelihood of a future settlement between those countries, and perhaps others as well, will be greatly enhanced.

But should the next round of talks and his mission fail, the U.S. peacemaking efforts and Sadat's opening to Israel will be at a dead end. Such a result would have grave consequences in the Middle East and throughtout the world.