The official welcome was warm, but there was unscripted discord from Sunni protesters angry over Iran's support for the regime of Syrian President Bashar Assad, as well as decades of sectarian animosity between Shia-led Iran and the region's Sunni majority.
In downtown Cairo, Ahmadinejad was forced to flee an ancient mosque after a Syrian protester took off his shoes and threw them at him.
Later, anti-Iranian protesters raised their shoes while blocking the main gates to Al-Azhar, the Sunni world's most prestigious religious institution, where Egypt's most prominent cleric chided Ahmadinejad for interfering in Sunni nations.
The protests illustrate the limits to how far and how quickly President Mohammed Morsi can go in reaching out to Iran: His Sunni allies at home view mainly Shia Iran as a bitter rival, and Cairo can't afford to alienate Washington and Gulf Arab states seeking to isolate Tehran.
The three-day visit, centered on an Islamic summit, were an attempt by Morsi to strike an independent foreign policy and reassert Egypt's historic regional leadership role following the ouster of Hosni Mubarak, a close U.S. ally who shared Washington's deep suspicions of Tehran. Such a visit by an Iranian leader would have been unthinkable under Mubarak.
Morsi gave Ahmadinejad a red-carpet welcome on the tarmac at Cairo airport. The two sat down for a 20-minute talk that focused on the civil war in Syria, security officials said. Egypt is among those that have called on Assad to step down.
Still, the chasm inherited from 34 years of bitter relations and the rift between Sunni Egypt and Iran's Shia leadership were on display.
A Syrian was arrested for the shoe-throwing incident outside the ancient al-Hussein mosque in downtown Cairo, according to security officials.
Sunni-Shia tensions also dominated talks with Egypt's most prominent cleric, Sheik Ahmed el-Tayeb, who upbraided the visitor on a string of issues and warned against interference in Gulf nations, particularly Bahrain.