Iraqis "need to take responsibility for their own country," Mr. Obama told hundreds of cheering soldiers gathered in an ornate, marble palace near Saddam Hussein's former seat of power.
"You have given Iraq the opportunity to stand on its own as a democratic country. That is an extraordinary achievement," he told the troops, saluting their efforts during six years of American fighting and losses.
Just hours before he arrived, a deadly car bomb exploded in Baghdad, underscoring the continuing peril despite a recent decline in violence. But the mood was festive as Mr. Obama spoke to troops gathered inside an ornate, marble palace.
"We love you," someone yelled out.
"I love you back," responded the president, repeating a sequence that played out at hundreds of campaign stops on his successful run for the White House last year. (Read the president's full remarks here.)
Obama met with top U.S. commanders as well as senior Iraqi leaders on a visit of a little more than four hours that was confined to Camp Victory, the largest U.S. military base in a war that began in 2003 and has cost the lives of 4,265 members of the U.S. military. Many thousands more Iraqis have perished.
The president said he had come "to say thanks to the troops." He added that the troops' stories "keep you going. They'll motivate you."
A helicopter flight to the heavily fortified Green Zone a few miles distant was scrapped, but White House aides attributed the change in travel plans to poor weather rather than security concerns.
The president's official schedule had said he would be flying directly to Washington D.C. from Istanbul on Tuesday and made no mention of an Iraq visit. Reporters did some sense that an unannounced trip was a strong possibility, however, as CBS News Producer Robert Hendin points out in a behind-the-scenes dispatch.
"It doesn't take one more than a second to look at a map of the Middle East to see that Turkey is pretty close to Iraq and Afghanistan," he notes. (Read more.)
After a session with Prime Minister Nouri al-Maliki, Mr. Obama said he had "strongly encouraged" Iraqis to take political steps that would unite political factions, including integrating minority Sunnis into the government and security forces.
Al-Maliki told reporters, "We assured the president that all the progress that has been made in the security area will continue."
Mr. Obama's predecessor, George W. Bush, made four unannounced trips to Iraq and two to Afghanistan during his presidency, reports CBS News White House Correspondent Mark Knoller. (Read more.)
American commanders told the president the country is experiencing a relatively low level of violence, although the car bomb explosion in a Shiite neighborhood of Baghdad was evidence of a recent resurgence. Mr. Obama flew from Turkey, the next-to-last stop on an eight-day itinerary that also included Britain, France, Germany and the Czech Republic.
Aides said the president chose to visit Iraq rather than Afghanistan, where U.S. troops are also in combat, in part because it was close to Turkey and in part because of upcoming Iraqi elections.
In his remarks to the troops, Mr. Obama made no mention of the Afghanistan conflict - where he has decided to commit 21,000 additional troops - and it was not known whether it came up in his meeting with Gen. Ray Odierno, the top U.S. commander, and other officers.
Mr. Obama announced plans in February to withdraw U.S. troops from Iraq on a 19-month timetable, although a force as large as 50,000 could remain at the end of that period to provide counterterrorism duties.
He said that for the next year and a half, the United States will be a "stalwart partner" to the Iraqis. And yet, he said, "they have got to make political accommodations. They're going to have to decide that they want to resolve their differences through constitutional means and legal means. They are going to have to focus on providing government services that encourage confidence among their citizens."
"All those things they have to do," said the president. "We can't do it for them."
By contrast, little more than a week ago, the president announced a revamped Afghanistan strategy that calls for stamping out the Taliban and al Qaeda and broadening the mission to include pressure on neighboring Pakistan to root out terrorist camps in its lawless border regions.
"We spend a lot of time trying to get Afghanistan right, but I think it is important for people to know that there is still a lot of work to do here," Mr. Obama said before Air Force One touched down in the Iraqi capital.
Even earlier, before departing Istanbul, the president told students, "Moving the ship of state takes time." Referring to his long-standing opposition to the war, he 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."
While Mr. Obama spent much of the past week overseas grappling with the worldwide economic crisis and the war in Afghanistan, a constant theme of the trip was his determination to turn a new page in U.S. relationships abroad after eight years of the Bush administration.
Nowhere was that intention more evident than in Iraq, where a Bush-ordered invasion in 2003 began as a quick rout of forces loyal to Saddam Hussein before gradually turning into a murderous environment for U.S. troops.
Mr. Obama said American forces had "performed brilliantly ... under enormous strain."
"It is time for us to transition to the Iraqis," he said as an estimated 600 troops cheered. "They need to take responsibility for their country."
In Europe, 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 promised 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," Mr. 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.
President Bush paid several trips to Iraq while in office, and on his last, in December, he had to duck shoes hurled in his direction at a news conference by an Iraqi journalist. By coincidence, the Iraqi Supreme Court reduced the prison sentence Tuesday for the man, Muntadhar al-Zeidi, now sentenced to one year in jail rather than three.
Audio: Obama Addresses The Troops