US President George W Bush has praised the progress of Afghan democracy on his first visit to the country, where the US helped eject the Taleban in 2001.
On a surprise first stop of his maiden trip to South Asia, Mr Bush told Afghan President Hamid Karzai his country was "inspiring others".
He said he was still confident Osama Bin Laden would be brought to justice.
Later Mr Bush headed to India, where tens of thousands of Muslims have protested against his visit.
In a sudden change to a schedule kept secret for security reasons, Mr Bush headed to Afghanistan before, rather than after, his visit to India.
Mr Bush arrived at the main US Bagram air base and was flown by helicopter to a red-carpet greeting from Mr Karzai at the presidential palace.
In a press conference with Mr Karzai, Mr Bush told the Afghan people their democracy was taking hold: "You are inspiring others and the inspiration will cause others to demand their freedom."
Mr Bush said he was still confident al-Qaeda leader Bin Laden and Taleban head Mullah Omar would be brought to justice, despite eluding coalition forces for four years.
"It's not a matter of if they are brought to justice, but when," he said.
Mr Bush also said Iran "must not have a nuclear weapon", saying that would be the "most destabilising thing that can happen in this region".
Dec 2001: Taleban ousted, Hamid Karzai interim president
Jan 2004: Grand Council adopts new constitution
Mar 2004: Afghanistan secures $8.2bn (£4.5bn) in aid over three years
Oct 2004: Mr Karzai wins presidential election
Sep 2005: First parliamentary and provincial elections in more than 30 years
2005: Militancy surges - the bloodiest year since the fall of the Taleban claims 1,400 lives
Feb 2006: Donors pledge more than $10bn in aid over five years
He later addressed about 500 members of the US 10th Mountain Division at Bagram air base.
"We will never be intimidated by thugs and assassins... We will win the war on terror," he told the cheering troops.
The BBC's Jonathan Beale, who is covering Mr Bush's trip, says although the president only spent a few hours on the ground in Afghanistan, he hopes the short visit will show moral support for Mr Karzai.
The US has about 20,000 troops hunting al-Qaeda and Taleban fighters in Afghanistan.
More than 130 US soldiers have died in combat there since the Taleban regime was ejected.
The US military says violence in Afghanistan went up 20% last year.
Taleban deputy leader Mullah Abdullah Akhund on Wednesday mocked the secrecy surrounding Mr Bush's visit.
"If the American president's visit had been announced in advance, the Taleban mujahideen would have greeted him with rockets and attacks," he told Reuters news agency by satellite telephone from an undisclosed location.
Mr Bush travelled from Afghanistan to India, where he hopes to finalise a landmark deal on sharing civilian nuclear technology.
The deal will give India much-needed access to US civilian nuclear technology.
But it has been held up by US demands that India should separate its civilian and military nuclear programmes, and open its civilian nuclear facilities to international inspectors.
Thousands of Indian Muslims demonstrated in Delhi on Wednesday against Mr Bush's trip.
Speakers accused him of being an enemy of Islam and said he was not welcome in India.
Tens of thousands also protested in the eastern city of Calcutta in a rally organised by 30 leftist groups.
Mr Bush will also visit Pakistan, and has said that he will try to press Delhi and Islamabad towards solving the long-running Kashmir dispute.
Mr Bush said in Kabul he would also raise with Pakistani President Pervez Musharraf the issue of militant incursions across the Afghan-Pakistan border.