In coming together, Mr. Erdogan and Mr. Morsi risk alienating their domestic political audiences by engaging so deeply with each another, analysts said. In Mr. Erdogan’s case, he may face criticism from the hard-line secularists who see themselves as the inheritors of the legacy of Mustafa Kemal Ataturk, the founder of modern Turkey, who forcibly imposed secularism. And for all the talk of Turkey’s presenting Egypt with a model for an Islamic democracy, many conservative Muslims in Egypt doubt the Islamic credentials of Turkey, where women who wear head scarves are still banned from working in government or running for office, analysts said.

“The Muslim Brothers are somewhat divided over Turkey as a role model,” Professor Soltan said. Some of the conservative members of the Brotherhood “have a vision for Egypt that is much more Islamic” than Mr. Erdogan’s party, he said.

But while there are some risks, both look out at the world as it is now and see little alternative, experts said.

In forming a partnership on security, economic and diplomatic matters with Egypt, its onetime rival, Turkey is advancing its efforts to shape the affairs of the Middle East while its dream of membership in the European Union, once its most important foreign policy initiative, seems more distant than ever.

Referring to Turkey and Egypt, Shadi Hamid, the director of research at the Brookings Doha Center, said, “Relations are warmer today than they have been in recent years, decades perhaps.” He added, “Turkey has become the effective leader of the Arab world, even though it’s not Arab.”

The scene at the annual convention of Mr. Erdogan’s party in Ankara, the capital, a few weeks ago offered a portrait of a realigned Middle East with Turkey at the helm. Mr. Morsi said at the gathering, “We offer our gratefulness for the support that the Turkish people and its administration has extended and will extend to us in the future.” To a standing ovation, Khaled Meshal, the political leader of Hamas, the Palestinian militant group, declared of Mr. Erdogan: “You are not only a Turkish leader. You are, now, also a leader of the world of Islam.”

Still, Turkey’s assertive role in the region is weighted by a history of Ottoman dominance over the Middle East, and resentments linger over the way the Ottomans treated the Arabs, said Paul J. Sullivan, a Middle East security expert at Georgetown University and a columnist for a Turkish newspaper. So, analysts say, the partnership could just as easily slip back into a rivalry for regional dominance, especially if Egypt can achieve political stability and engineer an economic recovery.