WASHINGTON — Eager to put the long and divisive Iraq war to rest, President Barack Obama declared Monday “those days are over” with the last American troops heading home, but he pledged the U.S. would remain committed to the fledgling government they leave behind. He and Iraq’s leader somberly saluted America’s war dead at Arlington National Cemetery.

“A war is ending,” the president said, standing with Iraqi Prime Minister Nouri al-Maliki at the White House. U.S. troops are leaving “with honor and with their heads held high,” said Obama, who strongly opposed the war as a candidate for the White House.

The last American troops are to be out of Iraq by Dec. 31. Thousands of others are still in Afghanistan.

Just 6,000 remain in Iraq, down from 170,000 at the war’s peak in 2007.

The withdrawal will cap a war in which nearly 4,500 Americans were killed, roughly 32,000 were wounded, hundreds of billions of dollars were spent and the American political debate was consumed until economic woes brought attention back home.

Obama had already said weeks ago that he was pulling all troops by year’s end, leaving his appearance with al-Maliki to focus instead on what’s next — a relationship both leaders described as rich in shared interests, from education to oil, politics to security.

To the Iraqi people, who still face massive challenges in rebuilding a society ripped apart by nearly nine years of war, Obama said: “You will not stand alone.”

The United States, in fact, needs the help of Iraq in dealing with the volatile Middle East and two of neighbors in particular, Iran and Syria. In getting out of Iraq, Obama emphasized that “our strong presence in the Middle East endures” and the United States won’t soften in its defense of its interests.

In the midst of a re-election run, Obama is using the war’s end to both honor the military’s sacrifice and to remind the nation the unpopular war is ending on his watch.

He is to deliver his war-is-over message in TV interviews on Tuesday and then again on Wednesday in remarks to troops at Fort Bragg, N.C.

Obama opposed the war from the start and eventually rode that stand to the White House.

In a 2002 speech during the months before the U.S.-led invasion of Iraq began, when Obama was a U.S. senator from Illinois, he that “What I am opposed to is a dumb war. What I am opposed to is a rash war.”

On Monday, speaking as a commander in chief, Obama put the focus on Iraq’s future.

“I think history will judge the original decision to go into Iraq,” Obama said. What’s clear, he added, is that because of the huge sacrifices by American soldiers and civilians and the courage of the Iraqi people, “we have now achieved an Iraq that is self-governing, that is inclusive and that has enormous potential.”

Said al-Maliki: “Anyone who observes the nature of the relationship between the two countries will say that the relationship will not end with the departure of the last American soldier.”

Early signs of how Iraq may orient itself could come from how it handles troubles in Syria, where the United Nations says 4,000 people have been killed in a government crackdown. The crisis has exposed differences in the U.S. and Iraqi positions: Obama says Syrian President Bashar Assad must step down. Al-Maliki has not.

“I do not have the right to ask a president to abdicate,” said al-Maliki. He suggested anew that Assad’s removal could lead to a civil war in Syria that could spread across the region and be difficult to control, calling for some other solution that would “avoid all the evils and the dangers.”

Obama said he and al-Maliki had “tactical disagreements” on Syria but shared the goals of free expression there without violence. Obama said he had no doubt that Iraq was acting in its own interests and not under the meddling influence of Iran. Tehran is Syria’s main backer.

Following their meetings at the White House, Obama and al-Maliki paid respects to the fallen at Arlington National Cemetery.