The long, costly story of American military involvement in Iraq moved closer to an end Thursday when Iraq's parliament approved a pact that requires all troops to be out in three years, marking the first clear timetable for a U.S. exit since the 2003 invasion that ousted Saddam Hussein.

The vote for the security deal followed months of tough talks between U.S. and Iraqi negotiators that at times seemed on the point of collapse, and then days of hardscrabble dealmaking between ethnic and sectarian groups whose centuries-old rifts had hardened during the first four years of the war.

The war has claimed more than 4,200 American lives and killed a far greater, untold number of Iraqis, consumed huge reserves of money and resources and eroded the global stature of the United States, even among its closest allies.

Now an end is in sight, and American troops could leave sooner if President-elect Barack Obama makes good on a plan to pull out combat troops within 16 months of moving into the White House in January.

Some troops are likely to redeploy to face an insurgency that has expanded in Afghanistan even as attacks have diminished in Iraq, where the U.S. believes Iraqi forces are better able to fend for themselves. The terms of the security pact reflect that confidence: U.S. forces will withdraw from Iraqi towns and cities by June 30 and the entire country by Jan. 1, 2012.

"This is a historic day for the great Iraqi people," Prime Minister Nouri al-Maliki said in a 10-minute address on national television. "We have achieved one of its most important achievements in approving the agreement on the withdrawal of foreign forces from Iraq and restoring the sovereignty it lost two decades ago."

Al-Maliki was referring to Iraq's transformation into an international pariah following Saddam's invasion of Kuwait in 1990, which led to U.N. sanctions and other penalties.

The security deal must now be ratified by the three-member Presidential Council, which is expected to approve it.

In the dealmaking that preceded the vote, Iraq's ruling Shiite bloc agreed to a Sunni demand that the pact be put to a referendum by July 30, meaning the deal could be rejected next year if, for example, anti-U.S. anger builds and demands for an immediate withdrawal grow. By that time, however, U.S. troops will likely have left urban areas and will be a less intrusive presence.

Under the pact, Iraq will have strict oversight over the nearly 150,000 American troops now on the ground, representing a step toward full sovereignty for Iraq and a shift from the sense of frustration and humiliation that many Iraqis feel at the presence of American troops on their soil for so many years.

President George W. Bush applauded the approval of the pact, which is divided into two agreements governing security, economics, culture and other areas of cooperation. He said it "affirms the growth" of democracy in Iraq and noted the impact of last year's "surge," or U.S. troop buildup.

"Two years ago, this day seemed unlikely," Bush said in a statement from his mountaintop retreat at Camp David, Md. "But the success of the surge and the courage of the Iraqi people set the conditions for these two agreements to be negotiated and approved by the Iraqi parliament."

The pact was backed by the ruling coalition's Shiite and Kurdish blocs and the largest Sunni Arab bloc, which wanted concessions for supporting the deal.

The Sunni bloc received assurances that the government would work to incorporate into the security forces the mostly Sunni fighters who had turned against Al Qaeda in Iraq. The government also agreed to stop pursuing fighters with alleged past links to the Sunni-led insurgency.

The Shiite-led government has previously made those assurances, but there were doubts about its commitment. Pledges of fair treatment were approved in a nonbinding vote in parliament on Thursday.

The 275-seat parliament voted on the security pact with a show of hands. There were conflicting figures for the number of deputies who attended the session, but most reports said three-quarters of up to 200 lawmakers in the chamber voted in favor.

The victory appeared to satisfy the guidelines of the country's most influential Shiite cleric, Grand Ayatollah Ali al-Sistani, who had indicated that the deal would be acceptable only if passed by a comfortable majority.

A bloc of 30 lawmakers loyal to Shiite cleric Muqtada al-Sadr, who wants U.S. forces to leave Iraq immediately, chanted protests and hoisted banners that said "No, no to the agreement" during the 25-minute session in parliament.

Al-Sadr's militiamen have fought American troops in major uprisings over the years, but the cleric largely disbanded his force and does not appear to pose as much of a security threat as in the past. Al-Sadr is currently in Iran.

Still, anti-American sentiment is likely to remain a flashpoint for discontent in Iraq, where many people suspect the United States will stay to preserve interests in the Middle East such as access to oil.

"I reject this agreement because it was signed under the occupation and was the result of external pressure and lowly political sectarian deals at the expense of the Iraqi people," said Qais Yassin, a Shiite engineer in eastern Baghdad, an al-Sadr stronghold.

Hussein Ali, a Shiite shop owner, said he thought the pact would ultimately have a positive outcome.

"The only thing we want is to live in peace and see the U.S. forces leave Iraq," he said.