Ahmadinejad, the first Iranian president to visit Egypt in more than 30 years, was given a red-carpet welcome by Islamist President Mohamed Morsi on Tuesday at the start of his landmark visit but was later chided by the top Sunni cleric.
Ahmed al-Tayyeb, the grand imam of Cairo's Al-Azhar, Sunni Islam's highest seat of learning, also denounced what he described as the "spread of Shiism in Sunni lands".
Tayyeb, who made the remarks in a statement after meeting Ahmadinejad, demanded "the Iranian president respect Bahrain as a brotherly Arab nation, and not interfere in the affairs of Gulf states".
He also said Ahmadinejad must uphold the rights of his Shiite-ruled country's Sunni minority.
In October, Bahrain summoned an Iranian envoy to protest against Tehran's "interference" in the Gulf state's internal affairs. Iran has supported protests by Bahrain's Shiite majority against the Sunni monarchy.
Following Tuesday's meeting, Ahmadinejad gave a news conference at Al-Azhar in which he said he "came from Iran to say that Egypt and the Egyptian people have their place in the heart of the Iranian people".
"I hope this visit will be a new beginning for solidarity between our two people," he said.
But senior Al-Azhar cleric Hassan al-Shafie, who spoke after Ahmadinejad, launched into a tirade against "some Shiites" for insulting some of the Prophet Mohammed's companions as the Iranian president listened with noticeable unease.
"The discussions were frank," Shafie said of Ahmadinejad's meeting with Tayyeb.
Shiites revile some of the Prophet Mohammed's companions they accuse of usurping power from his cousin Ali, whom they believe was designated as his rightful heir.
Sunnis view this position as heresy, but Al-Azhar had traditionally taken an ecumenical stance on Shiites.
But the Sunni institute has adopted a much harsher tone in the past year, accusing Shiites of trying to spread their doctrine in Egypt and even issuing a statement that used a pejorative term for Shiites -- rafidah, or rejectionists.
Al-Azhar's hardened stance is thought in part to stem from the increased pressure of more conservative Salafi clerics, who share doctrines of Saudi Arabia's interpretation of Sunni Islam.
In the evening, Ahmadinejad was booed by a man who also tried to throw his shoe at the Iranian president as he left the Al-Hussein mosque following prayers, according to a witness.
The man also pushed a bodyguard, but he was quickly dealt with and Ahmadinejad was able to enter his car.
In front of the mosque, four youths waved placards scrawled with slogans against Iran over its support for Syrian President Bashar al-Assad's regime in its 22-month conflict with Sunni-led rebels.
Ahmadinejad will attend a summit of the 57-member Organisation of Islamic Cooperation that opens Wednesday in Cairo to tackle crises ranging from the French-led battle against Islamist militants in Mali to the Syrian civil war.
The visit comes amid thawing relations between Egypt and Iran, which severed ties with Cairo in 1980 in protest over a peace treaty between Israel and Egypt in 1979, the same year Iran's Islamic revolution toppled the pro-West shah.
Iran has been reaching out to Egypt since Islamists came to power in the wake of the 2011 revolution that ousted veteran president Hosni Mubarak, a staunch critic of Tehran.
Morsi, who hails from the powerful Sunni Muslim Brotherhood, has attended a Non-Aligned Summit in Iran, becoming the first Egyptian president to travel to Tehran since the 1979 Islamic revolution.