The attacker, a 20-year-old wielding a knife, jumped out of a crowd that gathered to welcome the President on his arrival at North Thiladhunmathi atoll in Hoarafushi islet and tried to stab Gayoom in his stomach.
However, a 15-year-old boy scout, Mohamed Jaisham, grabbed the youth and wrestled with him before the attacker was captured, officials said.
"An unidentified man attempted to attack the President with a knife today," a statement from the President's Office in the capital Male said.
"The President escaped this serious attack without any harm. However, a boy scout was injured in the incident," the statement said.
According to Maldivian Information Minister Mohamed Nasheed, Gayoom was greeting people when the youth pulled out the knife.
"The president was greeting people when a young boy pulled out a knife and tried to stab him in the stomach," Information Minister Mohamed Nasheed said by telephone from the island capital of Male.
"The attacker had hidden the knife wrapped in a national flag," a spokesman for Gayoom's office said, adding that he was now in police custody.
He said Gayoom escaped unhurt from the incident, which took place on Hoarafushi in the north of the Maldives, a chain of 1,192 tiny islands.
The motive for the attack was not known, police said adding, investigations were being conducted.