It was a beautiful day in Baghdad — the sun bright and warm but not searing — as Robert M. Gates waxed nostalgic about his last visit to the nation that, more than any other, is likely to define his legacy as defense secretary.

“When I took this job, I was asked what my agenda was, and I said, ‘Iraq, Iraq and Iraq.’ And political heat was quite hot in Washington. Things were not going well here,” Gates, dressed in khakis and an open-collared dress shirt, told a group of journalists Thursday. “When I gave my first press conference here in December of 2006, there was a firefight going on overhead and in the background. And then in the spring, we were losing up to 140 soldiers and Marines a month. It was a very tough time.”

He said the change since then has been “night and day.” Gates made similar points throughout the day as the Iraqi leg of a five-day Middle Eastern visit took on the feel of a valedictory tour. He told Prime Minister Nouri al-Maliki that “a lot has changed for the better.” And when Gates reminded President Jalal Talabani that he had visited Iraq many times as defense secretary, Talabani replied, “Yes, I remember.”

Iraq is hardly a bastion of peace as it prepares for the withdrawal of U.S. troops amid lingering security concerns and political turmoil. The government does not have defense or interior ministers. There have been street protests in recent months. And the military has such substantial shortages in air power and other basics that U.S. officials worry openly that it couldn’t fight off a foreign enemy.

Yet for all the uncertainty, Baghdad no longer ranks among the region’s most troubled capitals. As Gates noted wryly, his agenda now overflows with concerns about Libya, Afghanistan, Egypt, Bahrain and more.

What’s left of the U.S. military presence here — about 47,000 troops, less than a third of its peak — is preparing to leave by the end of the year. Since the halting of combat operations last year, there have been 12 American deaths, equal to the toll of a few ordinary days during the height of the fighting.

While Iraqi security forces are relatively well-trained, they remain largely under the control of Maliki, who has been accused of showing an authoritarian streak and deploying them against political enemies. And while security has improved, that is largely a function of a fragile governing coalition that brought militants such as the radical Shiite cleric Moqtada al-Sadr into the fold. The bargaining power of the political blocs lies to a large extent in the threat of returning their militias — now folded into the army and police — to the streets.

For those reasons, many ordinary Iraqis are skeptical about the government’s ability to maintain security if the U.S. forces depart, particularly in highly volatile areas such as Kirkuk. “It’s impossible,” said Ali Timimi, an activist who helped to lead recent protests aimed at government reform. “This whole military, and the country, is now depending on political parties that are not in agreement.”

But asking U.S. forces to stay would be difficult. “Politically, we have to say, ‘Yes, we are going to fulfill this agreement,’ ” said Abdul Hadi Alhassani, a member of parliament from Maliki’s political bloc. “However, really, we need to be frank and honest about security, to prevent the worst.”

The question of the looming transition was central in the talks Gates had with Iraqi leaders in his 13th visit to the country as defense secretary. He is urging them to decide what — if any — U.S. force they would like to see remain behind after the deadline for withdrawal at the end of the year.

The State Department will take on an expanded role in training police officers, but some military officials are angling for a modest role as well, probably in efforts to support external defense. There also are concerns about the persistence of al-Qaeda in Iraq, which took credit for a bombing last week that killed more than 57 people in Tikrit.

“I think there is an interest in having a continued presence,” Gates told a gathering of U.S. troops. “But the politics of it is, we’ll have to wait and see, because the initiative has to come from the Iraqis.”

Gates, who was retained as defense secretary by President Obama, had been appointed by President George W. Bush after he fired Donald H. Rumsfeld following a Republican drubbing in the 2006 midterm elections. In the post, Gates became a crucial advocate for sustaining the troop surge that many credit with turning around the war effort. Along the way, he pushed a reluctant Pentagon bureaucracy to produce new mine-resistant vehicles and to bolster aerial surveillance for better battlefield intelligence.

Gates has not said when he will leave office, other than that it will be this year. But he has made clear he regards Iraq’s relative stability as a signature accomplishment.

“A lot of people, journalists and others said my time in this job . . . would be evaluated by how Iraq turned out, because that was the issue,” Gates said. “Well, I’ll let people judge for themselves.”

Staff writer Stephanie McCrummen contributed to this report.