Media reports quoted him as saying that Gayoom was unhurt after the incident and that the attacker was in custody.
"The knife did catch [Gayoom's] shirt," Shareef said.
"He was saved by a boy who moved in the way and tried to grab the knife and suffered a serious injury to his hand."
Speaking from Male, the capital, Mohamed Nasheed, the information minister, said: "The president was greeting people when a young boy pulled out a knife and tried to stab him in the stomach."
The motive for the attack was not known, police said.
They said investigations are being conducted.
The Indian Ocean island, a favoured destination for tourists and honeymooners, has been tense since a bomb attack in September last year injured 12 foreign tourists.
Three men convicted of carrying out the bombing were last month each jailed for 15 years.