Gomaa was on his way to a mosque near his house in the October 6 suburb when the assailants hiding in a park started shooting, the ministry said.
His guards exchanged fire with the gunmen and forced them to flee, it said, adding that one of the guards was lightly wounded.
Gomaa later told Egyptian state television of how he had taken shelter behind a mosque wall when the shooting started.
After the attack, he "conducted the Friday prayers as a message to these people", the cleric said in a telephone interview.
More From This Section
The private CBC Extra news channel showed footage of armed guards with pistols drawn escorting Gomaa out of the mosque after the shooting.
In an interview with the channel, Gomaa said this was not the first attempt on his life and derided the assailants as "very stupid".
"They blew up my house in Fayoum," he said, referring to a province southwest of Cairo. And assailants had also "tried before from the park" outside his Cairo home.
Gomaa was the mufti - the government's official interpreter of Islamic law - for a decade until 2013.
Since stepping down as mufti, Gomaa has remained one of the country's top Islamic scholars.
Gomaa is known for his moderate views on religion but is reviled by the Islamist opposition for supporting the crackdown against them that has killed hundreds of protesters.
He is a member of the council of senior scholars in the Al-Azhar institute, the top body in Sunni Islam's most prestigious centre of learning, and has hosted a religious affairs programme on television.