Stocks closed way off session lows on Thursday on news Greece agreed to a five-year austerity plan, but lingering economic uncertainty ultimately drove the S&P 500 lower, keeping a downward trend in place.

Nearly 30 percent of the day's volume traded in the last hour. That coincided with the market's turnaround and the news out of Greece, which set the stage for a resolution to Athens' credit problems that have hurt investor sentiment around the globe.

The agreement on the austerity plan, that's really what got the market going, said Paul Hickey, co-founder of Bespoke Investment Group in Harrison, New York.

The question now remains whether the late-day surge is a precursor to renewed buying interest or if it was just an interruption before selling returns in coming days.

We're going headline to headline, which is typical of a market pullback, Hickey said. There's not a whole lot of tangible evidence for investors to move on right now.

The S&P 500 came within less than a half point of its 200-day moving average -- a line the bulls have been able to hold since last September. Technical analysts monitor that level as an indication of the long-term trend, and a consistent close below it could trigger more selling.

It's definitely positive and encourages people, Hickey said.

Sources with knowledge of the talks told Reuters that Greece has won the consent of a team of European Union and International Monetary Fund inspectors for its new five-year austerity plan after committing to an additional round of tax increases and spending cuts.

The Dow Jones industrial average <.DJI> dropped 59.67 points, or 0.49 percent, to end at 12,050. The Standard & Poor's 500 <.SPX> lost 3.64 points, or 0.28 percent, to 1,283.50. But the Nasdaq Composite <.IXIC> gained 17.56 points, or 0.66 percent, to close at 2,686.75.

A Bespoke analysis showed the S&P 500 posted the strongest comeback in almost a year, on days when the benchmark has fallen more than 1 percent. From its session low, the index climbed more than 20 points into the close.

The Dow's swing covered 233.79 points from intraday low to session high. For the second straight day, the Nasdaq managed to end the session in positive territory for the year.

MICRON AND ORACLE FALL LATE

After the closing bell, Micron Technology shares tumbled nearly 13 percent as the company's quarterly revenue fell below expectations.

Oracle Corp shares dropped 3.3 percent in extended trading as investors were disappointed that its profit beat estimates by a narrower margin than in recent quarters.

Earlier in the day, markets had sold off during the regular session as oil's slide to a four-month low triggered declines in a market already hurting after Federal Reserve Chairman Ben Bernanke's comments on Wednesday about a slowing economic recovery.

Skepticism remained despite the Greek deal as details were not yet known and any agreement would still have to win a vote in Parliament.

When it's 3 o'clock on a Thursday afternoon and short-sellers see that (Greece deal) headline, they cover first and ask questions later, said Peter Boockvar, equity strategist at Miller Tabak & Co in New York.

He said since there was already a plan awaiting a vote, the difference could be a change in terms that could make it easier to pass in the Athens Parliament next week.

U.S. crude oil futures settled at $91.02 a barrel, down $4.39 or off 4.6 percent, after the International Energy Agency said it will release 60 million barrels of oil from strategic stockpiles. For details, see

The slide in the price of oil was exacerbated by a 0.7 percent climb in the U.S. Dollar Index <.DXY>, which tracks the greenback's performance against a basket of major currencies. In times of stress, the flight to safety that pushes the dollar higher makes oil more expensive, further sapping demand for crude and other commodities priced in dollars.

Giving some support to the market, tumbling oil prices lifted an index of airlines' stocks <.XAL> by 2.4 percent.

Bets that lower prices at the pump will open consumer's wallets boosted the S&P retail sector index <.RLX> by 1.4 percent.

S&P BOUNCES OFF KEY SUPPORT

The S&P 500's bounce off its 200-day moving average was the second in a week. Last Thursday, a brush with that level enticed buyers and the benchmark index closed in the black for the day. The 200-day moving average now coincides with the 2010 intraday high of 1,262.60, giving it extra technical support.

What it means is that if you do momentum trading, you can place bets, and a lot of big money is doing momentum trading, said George Feiger, CEO of Contango Capital Advisors in San Francisco.

But the big picture story remains the same. We are looking at small growth and very small return in equities for the coming years.

His view differs from the median of 46 equity strategists surveyed in the last week, which showed an expectation of an 11 percent gain in the S&P 500 for the year, which would take it to 1,400.

On the economic front, U.S. claims for unemployment benefits rose more than expected last week, suggesting little improvement in the labor market. Other data showed sales of new homes fell in May.

About 8.31 billion shares traded on the New York Stock Exchange, NYSE Amex and Nasdaq, above the daily average so far this year of 7.57 billion.

Declining stocks outnumbered advancing ones on the New York Stock Exchange by a ratio of 17 to 13. But on the Nasdaq, about seven stocks rose for every six that fell.

(Reporting by Rodrigo Campos; Additional reporting by Ashley Lau; Editing by Jan Paschal)