WASHINGTON — President Obama announced on Friday that the last American soldier would leave Iraq by the end of this year, drawing to a close a divisive eight-year war that cost the lives of more than 4,400 troops, defined the presidency of George W. Bush, and helped ignite his own political rise.

The decision leaves only a vestigial presence of Marine embassy guards and liaison officers staying on where more than a million troops, in all, have served.

The president’s statement, coming a day after a NATO air campaign hastened the death of Col. Muammar el-Qaddafi in Libya, was laden with symbolism, marking the ebb tide of a decade of American military engagement that began after the Sept. 11 terrorist attacks. It also capped a remarkable period of foreign-policy accomplishments for a president who is hindered by a poor economy at home.

For Mr. Obama, whose rise to the White House was based partly on his opposition to the Iraq war but who as president ordered a troop buildup in Afghanistan and intensified drone strikes against militants in the region, the announcement fulfills a campaign promise. Its timing, after Colonel Qaddafi’s death and the commando raid that killed Osama bin Laden, and just as the administration was taking its toughest stance yet on Pakistan’s reluctance to root out militants along its border with Afghanistan, may help insulate him from Republican charges that he is weak on national security.