U.S. stocks ended 2009 on Thursday with their best gains since 2003, with optimism about the economy's recovery pushing the benchmark Standard & Poor's index up 23.5 percent for the year.

For Thursday's session alone, though, U.S. stocks declined, with a late-day sell-off pushing all three major indexes down about 1 percent as investors sold some of the year's best-performing stocks to lock in some of 2009's substantial gains.

Most of the year's advance is the result of a nine-month rally that has been underpinned by strength in technology and natural resource shares on expectations the economic recovery will spur capital spending and increase demand for energy, metals and other natural resources.

The S&P 500's top-performing stock for the year was XL Capital -- up an eye-popping 395.4 percent in 2009. On Thursday, however, XL Capital's stock slipped 0.4 percent to end at $18.33 on the New York Stock Exchange.

Signs of an economic rebound, including more than 70 percent of companies beating profit expectations in the second quarter, have driven the S&P 500 up 65 percent since its March 9 closing low.

It really was a turnaround year, said Charles Lieberman, chief investment officer of Advisors Capital Management, LLC in Paramus, New Jersey. It shows how much of a recovery there's been.

After a fast sell-off late in the session, the Dow Jones industrial average <.DJI> ended down 120.46 points, or 1.14 percent, at 10,428.05. The Standard & Poor's 500 Index <.SPX> slid 11.32 points, or 1.00 percent, at 1,115.10. The Nasdaq Composite Index <.IXIC> lost 22.13 points, or 0.97 percent, to close at 2,269.15.

For the year, though, the Dow climbed 18.8 percent, while the Nasdaq is up 43.9 percent from its close on December 31, 2008.

It was the market's first annual advance in two years.

In 2008, the S&P 500 slid 38.5 percent when the economic crisis led to Wall Street's worst year since the Great Depression. (Reporting by Caroline Valetkevitch; Editing by Jan Paschal)