The dramatic trip came as Mr. Bush sought to bolster support for Iraq's fledgling government and U.S. war policy at home.
"I've come to not only look you in the eye. I've also come to tell you that when America gives its word, it keeps its word," Mr. Bush told al-Maliki.
Later, Mr. Bush told a group of cheering American troops that the war in Iraq "will go down in history books as an incredibly important moment" for freedom, democracy and America's safety.
"I've come today to personally show our nation's commitment to a free Iraq," he said. "My message to the Iraqi people is this: seize the moment, seize the opportunity to develop a government of and by and for the people."
"I come away from here believing that the will is strong," he added.
After his talk to the U.S. troops, Mr. Bush flew by helicopter back to Air Force One under the cover of darkness for the flight back to Washington.
Mr. Bush traveled to Baghdad less than a week after a U.S. air strike killed terror chief Abu Musab al-Zarqawi. It was his second unannounced visit since the 2003 invasion that toppled Saddam Hussein.
The trip was known only to a handful of aides and a small number of journalists sworn to secrecy because of obvious security threats for Mr. Bush and members of his entourage.
CBS News White House correspondent Peter Maer reports some top White House officials and even Cabinet secretaries are in a state of shock over the president's trip. Except for Vice President Dick Cheney, Secretary of State Condoleezza Rice and Defense Secretary Donald Rumsfeld, no Cabinet members knew about the trip until Tuesday morning.
Al-Maliki, who didn't know Mr. Bush was coming until five minutes before they met, said that Iraq was "determined to succeed, and we have to defeat terrorists and defeat all the hardships."
"God willing, all the suffering will be over. And all the soldiers will return to their country with our gratitude for what they have offered, the sacrifice," al-Maliki said through a translator.
Mr. Bush made it clear, however, that a U.S. military presence — now at about 132,000 troops — would continue for awhile.
"I have expressed our country's desire to work with you, but I appreciate you recognize the fact that the future of the country is in your hands," Mr. Bush said.
"The decisions you and your cabinet make will determine as to whether or not your country succeeds, can govern itself, can defend itself, can sustain itself," he added.
"I am impressed with the cabinet you have assembled," Mr. Bush said.