Karzai said that "the enemy of Afghanistan" tried to disrupt the ceremony but were thwarted by security forces.
A statement from the presidential palace said Karzai, all Cabinet members and diplomats were safe. But a local Shiite Muslim leader was killed by a gunshot to his heart, and nine others, including two lawmakers, were wounded. The lawmakers were undergoing surgery, he said.
About 100 people were rounded up for questioning, an Afghan intelligence official said. He spoke on condition of anonymity because he was not authorized to speak to media. The government has yet to identify any suspects.
A Taliban spokesman claimed responsibility for the attack, saying it had deployed six militants with suicide vests and guns to target the president. Spokesman Zabiullah Mujaheed said three had died.
A police official, who requested anonymity because of he was not authorized to speak to media, said security forces killed three gunmen who had opened fire from an apartment block not far from the ceremony and confiscated assault rifles and machine guns. Government officials could not immediately confirm that information.
Hundreds of people fled in chaos as shots rang out, just as the national anthem ended at a ceremony to mark the 16th anniversary of Afghanistan's victory over the Soviet invasion.
The gunfire appeared to come from ruined houses about few hundred yards from where the VIPs were seated. Security forces deployed elsewhere opened fire at the houses.
Karzai was escorted from scene, surrounded by bodyguards, in one of four black Landcruisers. A U.S. embassy official said U.S. Ambassador William Wood also escaped unharmed.
"President Karzai condemns this act and asks for all the people to remain calm," a statement from the presidential palace said.
Karzai, who has led Afghanistan since soon after a U.S.-led invasion ousted the Taliban regime in 2001, has been targeted by assassins before and is constantly shadowed by a phalanx of bodyguards.
The attack came despite unprecedented tight security for Sunday's celebrations.
For days Kabul has been ringed by checkpoints with security forces and plainclothes intelligence officials searching vehicles. The area where the ceremonies took place had been blocked off by troops, tanks and armored personnel carriers.
The live TV coverage of the assassination attempt will add to the sense of insecurity in the Afghan capital, which has been spared the worst of the violence as fighting has escalated between Taliban insurgents and NATO and U.S.-led forces -- leaving thousands dead last year.
It was the first militant attack in the city since mid-March.
In TV footage, two lawmakers who were sitting about 30 yards from Karzai appeared to be hit by the gunfire. One of the men slumped back in his seat, while the other lay on the ground.
People at the ceremony ducked for cover then fled -- among them Afghan police and soldiers who were assembled for the pageantry. Karzai had just completed a drive-past in a U.S.-supplied Humvee jeep.
Mujaheed, the Taliban spokesman, said insurgents carrying AK-47s and rocket-propelled grenades perpetrated the attack. He said BM-12 missiles -- a crude rocket launched from a small platform -- were used in the attack. He spoke to an AP reporter by phone from an undisclosed location.
Mohammad Saleh Saljoqi, a lawmaker at the ceremony, said two rockets -- which he described as rocket-propelled grenades -- landed near the dignitaries.
One rocket hit inside the Eid Gah mosque opposite where Karzai was sitting. The second hit when the president had already left, landing about 50 yards away, Saljoqi said.
Sirajudin, a police officer at the scene, said he saw two people firing AK47 assault rifles from a house toward the area where Karzai and other dignitaries were sitting.
Karzai's narrowest escape from an assassination attempt since he became president came in September 2002 when a gunman opened fire as he visited the southern city of Kandahar. Three people, including the gunman, died in that attack.