President Obama all but declared victory in Iraq Tuesday, telling U.S. troops during a surprise visit to Camp Victory that they had given the Arab nation an opportunity to stand on its own as a democratic country. That, he said, is an extraordinary achievement.

"Here's a couple things I want to say. Number one, thank you," the president bellowed as he addressed troops at a stopover at the tail end of a marathon overseas trip. After capping his visit with meetings with top U.S. military and Iraqi officials, he then left Baghdad on Air Force One for Washington. 

Obama told the troops the next 18 months are critical to the mission. U.S. troops are expected to be out of the country by the end of 2010.

"You've kept your eyes focused on just doing your job and because of that, every mission that's been assigned, from getting rid of Saddam to reducing violence to stabilizing the country to facilitating elections, you have given Iraq the opportunity 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.

Obama also met with Iraqi Prime Minister Nouri al-Maliki, who said afterward that he had "assured the president that all the progress that has been made in the security area will continue." 

Obama said he had "strongly encouraged" the Iraqi leader to take steps to unite political factions, including integrating Sunnis into the government and security forces.

Obama, who opposed the war in Iraq, said earlier that it is time for the Iraqis to "take responsibility for their country and for their sovereignty," which will require political accommodations. He said the U.S. "can't do it for them but what we can do is make sure that we're a stalwart partner that we are working alongside them."

Obama said U.S. troops will be committed to training security and civilian forces, which will help Iraq achieve a more effective government. He said the Iraqis will know that "they have a steady partner with us."

"You will be critical in terms of us being able to make sure that Iraq is stable that it is not a safe haven for terrorists, to make sure that they're a good neighbor and a good ally and we can start bringing our folks home," he said.

Obama spoke for just six minutes with the nearly 700 troops who gathered at the base that was formerly a palace for Saddam Hussein. Obama met with National Security Adviser James Jones; the head of command in Iraq Gen. Ray Odierno; and others before walking out into the glitzy, over-the-top main room where troops gathered under a huge chandelier and intricate gold work.

Obama's appearance pleased troops interviewed by reporters. They said they were gratified the new president came to visit so soon after taking office. 

"We love you," shouted a few troops. The president shouted he loved them back.

"We have not forgotten what you've already done. We are grateful for what you will do, and as long as I'm in the White House you're going to get the support that you need and the thanks that you deserve from a grateful nation. So thank you so much everybody, God bless you, God bless the United States of America," he said.

Obama landed in Baghdad late Tuesday afternoon for his first trip to Iraq as president -- his third overall. His visit comes as he begins to execute plans to shift U.S. troops from Iraq, a war he opposed as a candidate, to Afghanistan

"There is still a lot of work to do here," Obama said, but he praised the "significant political progress" he's seen in Iraq. 

White House Press Secretary Robert Gibbs said Obama chose to visit Iraq rather than Afghanistan because it was closer to Turkey, where he was flying from, and provides Obama to opportunity to consult with Iraqi officials about critical "political solutions." Gibbs said Obama also wanted to visit troops still fighting in the country. 

"Our men and women who are in harm's way, either in Iraq or Afghanistan, deserve our utmost respect and appreciation," Gibbs said. 

Obama flew into the country hours after a car bomb exploded in a Shiite neighborhood of the capital city, a deadly reminder of the violence that has claimed the lives at least 4,266 members of the U.S. military since March 2003. 

In his briefing, Odierno told the president that despite the recent uptick in violence, bombing incidents are still at their lowest since the war began. 

The visit came at the conclusion of a long overseas trip that included economic and NATO summits in Europe and two days in Turkey. 

Shortly before leaving Turkey, the president held out Iraq as an example of the change he seeks in policies inherited from former President George W. Bush

"Moving the ship of state takes time," he told a group of students in Istanbul. He noted his long-standing opposition to the war, yet said, "Now that we're there," the U.S. troop withdrawal has to be done "in a careful enough way that we don't see a collapse into violence." 

In office only 11 weeks, Obama has already announced plans to withdraw most U.S. combat troops on a 19-month timetable. The drawdown is to begin slowly, so American forces can provide security for Iraqi elections, then accelerate in 2010. As many as 50,000 troops are expected to remain in the country at the end of the 19 months to perform counterterrorism duties. 

Tuesday's trip was Obama's third to Iraq overall. He met with U.S. commanders and troops last summer while seeking the presidency. 

Because of security concerns, the White House made no advance announcement of the visit, and released no details for his activities on the ground. 

It was the last stop of an eight-day trip in which Obama sought to place his stamp on U.S. foreign policy. He and other world leaders pledged cooperation to combat a global recession, and he appealed with limited success for additional assistance in Afghanistan, a war he has vowed to intensify. 

The new president drew large crowds as he offered repeated assurances that the United States would not seek to dictate to other countries. 

"I am personally committed to a new chapter of American engagement. We can't afford to talk past one another, to focus only on our differences, or to let the walls of mistrust go up around us." Obama said before leaving Turkey. The visit to a nation that straddles Europe and Asia was designed to signal a new era. He had pledged as a candidate to visit a majority-Muslim nation in his first 100 days in office. 

FOX News' Major Garrett and The Associated Press contributed to this report.