U.S. stocks gained on Thursday after surprisingly strong March retail sales increased optimism that the economic recovery is on track.

Investors snapped up retailers' shares after top U.S. chains reported a record year-over-year increase in same-store sales for March. The sales reflected a boost in consumer demand that some investors had doubted would materialize, with job growth still anemic.

The S&P retail index <.RLX> rose 1.3 percent. Online merchant Amazon.com jumped 4.4 percent to $140.77, its highest close in four months.

Retail sales are the whole backbone of the entire recovery, said Keith Springer, president of Capital Financial Advisory Services in Sacramento, California.

The whole premise is, if consumer spending and retail sales maintain anywhere close to where they were a couple years ago, then that will be a true recovery.

The Dow Jones industrial average <.DJI> gained 29.55 points, or 0.27 percent, to 10,927.07. The Standard & Poor's 500 Index <.SPX> rose 3.99 points, or 0.34 percent, to 1,186.44. The Nasdaq Composite Index <.IXIC> added 5.65 points, or 0.23 percent, to 2,436.81.

The indexes' moderate gains were characteristic of the rally's most recent leg. The S&P 500 has ground slowly higher since the beginning of March, rising 7.4 percent, even as momentum indicators suggest the rally could stall in coming weeks.

The biggest thing in the market at the moment is, the path of least resistance is higher, said David Katz, chief investment officer of Matrix Asset Advisors in New York. You've had an improving market for the last month-and-a-half, and you have people that were under-invested that seem to be jumping on that bandwagon.

Among advancers in the retail space, Target Corp rose 3 percent to $55.64, while Gap Inc climbed 3.1 percent to $24.59.

Major retail chains reported a record 9.1 percent jump in March same-store sales -- beating the forecast for a 6.3 percent gain, according to Thomson Reuters data. Of the 28 retailers that Thomson Reuters tracks, more than 90 percent topped estimates.

Even so, retail analysts warned that the jump in March would come at the expense of April sales.

Gains in retailers' stocks eclipsed worries about Greece's debt load as the country's borrowing costs rose to new highs even as the government struggled to reassure markets it can stay solvent.

An index of U.S.-listed stocks of Greek companies <.BKGR> fell 2.2 percent.

In a move that could create the second-largest air carrier in the United States, UAL Corp's United Airlines and US Airways Group were in merger talks, according to two sources familiar with the matter.

Shares of both companies surged, with UAL Corp up 6.8 percent at $20.23 and US Airways jumping 10.7 percent to $7.55.

On the economic front, data showed the number of workers filing for unemployment benefits unexpectedly rose last week. But the jump reflected volatility from the Easter holiday and did not alter the view that labor markets are recovering.

(Reporting by Leah Schnurr; Editing by Jan Paschal)