US President Barack Obama has said the time has come for Iraqis to "take responsibility for their country".
Mr Obama was speaking during an unannounced trip to Baghdad, his first visit to the country as president.
He was greeted enthusiastically by US troops, whom he praised for their "extraordinary achievements" in Iraq.
Mr Obama also had talks with Iraq's leaders, and confirmed his plans to withdraw all US troops from Iraq by the end of 2011.
The president flew into Baghdad's international airport before travelling by road to meet Gen Ray Odierno, the US military commander in Iraq, and some of the 140,000 US soldiers serving in the country.
The Iraq visit is being described as a surprise when it is anything but
Mr Obama thanked Gen Odierno for helping to lead what he said was "a very effective operation" in Iraq.
He told about 600 US soldiers gathered at the Camp Victory military base that they had helped Iraq to "stand on its own as a democratic country".
"That is an extraordinary achievement, and for that you have the thanks of the American people," he said.
The troops had greeted him with cheers and shouts of "we love you Obama".
Mr Obama and Mr Maliki had a "positive" meeting, said aides
Mr Obama told the soldiers that the next 18 months "could be a critical period" in Iraq.
He has said he wants US soldiers to have left Iraqi cities by the middle of summer this year, and most of them to be out of the country altogether by the end of 2010.
A smaller number of military advisers and support staff would remain in the country until 2011.
"It is time for us to transfer to the Iraqis. They need to take responsibility for their country," he said, before shaking hands and taking photographs with many of the soldiers.
The visit, which lasted less than five hours, also included a meeting with Iraqi Prime Minister Nouri al-Maliki, who travelled to Camp Victory to meet Mr Obama.
A spokesman for Mr Maliki said the meeting had been "positive" and that Mr Obama had "renewed the American commitment to Iraq and to withdrawing troops as previously planned".
Mr Obama told Mr Maliki that progress had been made on improving security but that it was "absolutely critical" for all Iraqis to be integrated into the political system.
He said the US had "no claim on Iraqi territory and resources," reported Reuters.
Mr Obama was greeted enthusiastically by US soldiers at Camp Victory
For security reasons, the White House had made no advance announcement of the visit and did not release details of the president's schedule.
But the BBC's Jim Muir in Baghdad said there had been widespread speculation that Mr Obama would visit either Iraq or Afghanistan at the end of his Europe trip, which included economic and Nato summits and a visit to Turkey.
US officials said Mr Obama chose Iraq over Afghanistan partly because Iraq was simply closer to Turkey, but also to show Iraqi leaders that the way forward for the country lies "in political solutions".
Mr Obama said the US had "spent a lot of time trying to get Afghanistan right" but that there was "still a lot of work to be done" in Iraq.
He said his presence in Iraq could also help the country's competing political factions to reach "equitable" solutions to the issues facing Iraqis.
Levels of violence have been generally declining in Iraq, but Mr Obama's visit comes a day after more than 34 people died in apparently coordinated car bomb attacks in Baghdad.
Insurgents still seem able to strike with deadly results, claiming more than 200 lives in both February and March.
Some US military and Iraqi government officials have privately expressed concern that the violence might again rise as US troops are pulled out.
Our correspondent says Mr Obama will have used the visit to reassure Iraq's leaders that, when US troops are eventually withdrawn, it will be done responsibly and without plunging the country into chaos.
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.