The rocket exploded near a school about 2km from where the US military helicopter in which he was travelling was preparing to touch down. There were no injuries.
Karzai was named interim president in 2002 after a US-led invasion toppled the Taliban government.
He faces 17 rivals in the 9 October vote - the first direct presidential poll in Afghan history.
The Taliban has vowed to disrupt the elections, which the US-backed incumbent is favourite to win, and has threatened all candidates.
Karzai narrowly escaped an assassination attempt on 5 September 2002, in the southern city of Kandahar, after which his security was dramatically tightened.
He has rarely been seen in Afghanistan outside his heavily fortified presidential palace where he is protected by US bodyguards.
Presidential spokesman Jawed Ludin said Karzai had returned safely to Kabul and the incident was under investigation.
He said the rocket landed in the village of Rabat at about the same time Karzai's helicopter was coming in nearby, but it would be premature to say it was an assassination attempt.
"It's too early to say who did it and what the motivation was ... It would be premature to conclude there was a direct link to the president landing there"
"It's too early to say who did it and what the motivation was," he said. "It would be premature to conclude there was a direct link to the president landing there.
"It was really quite a long distance away from the landing ground, but the security obviously reacted and made a decision not to land.
"The president is disappointed not to have been able to attend the occasions he wanted to attend."
The Gardez trip was a rare visit to the provinces by Karzai.
A spokesman for the US-led military force in Afghanistan said it had no details of the incident. "We are investigating," he said.
Official campaigning for the election has been largely low key with none of the large rallies and parades seen in other countries.
The poll is seen as a crucial test of US nation-building efforts ahead of President George Bush's own bid for re-election in November.
More than 10.5 million Afghans
have registered to vote
The vote has been delayed twice, partly because of growing insecurity. About 1000 people including aid workers, fighters, civilians and Afghan and foreign troops have been killed in violence in the past year.
More than 10.5 million people from a population of between 25 million and 28 million have registered to vote, far surpassing expectations but leading to allegations of multiple registrations.
Victory requires 51% of the vote, otherwise a run-off will be called, which could delay the result until November.