Mr. Morsi, an Islamist and Egypt’s first elected leader, promised a new direction for foreign policy that he said would be more independent than his predecessors’ and would reassert Egypt’s historical leadership role. Extending a hand to Iran was seen as part of an effort to improve ties with regional powers and, more important, to broker a solution to the war in Syria.

Iranian officials were even more eager to mend the relationship, speaking of Egypt and Iran as the core of an axis of regional military powers. Iran’s supreme leader, Ayatollah Ali Khamenei, has been promoting the idea that the recent Arab uprisings were inspired by the Iranian revolution of 1979.

The first major step toward repairing the relationship came in August, when Mr. Morsi visited Tehran during the meeting of the Nonaligned Movement, in what was seen as an effort toward easing Iran’s international isolation.

But that trip also veered off script. Mr. Morsi embarrassed his hosts somewhat by delivering a stinging condemnation of President Assad, delighting Saudi Arabia and Qatar, strong financial supporters of both Egypt and the Syrian insurgency.

And in speaking about the Arab uprisings, Mr. Morsi notably avoided, as he often does, any mention of the Shiite-led revolt against Bahrain’s Sunni monarchy, further endearing him to allies in the gulf region who have portrayed the uprising as an Iranian plot rather than a popular movement.

Many analysts think it unlikely that the Egypt-Iran thaw will develop into fully restored relations, given the pressure exerted by the United States and the gulf monarchies on Mr. Morsi to keep his distance. Despite his talk of charting a new path for Egypt, “the Morsi administration is trying to avoid any direct confrontation with the international system,” said Emad Shahin, a political scientist at the American University of Cairo.