U.S. machinery maker Caterpillar Inc posted stronger-than-expected second-quarter earnings and raised its full-year outlook, citing signs of global economic stability, sending its shares up as much as 13 percent.

But it later warned that the third quarter would be tough for sales and production and that it could report a loss for that period, and its shares erased most of their earlier gains.

Caterpillar's stock was up 4.37 percent at $38.25 in afternoon trading on the New York Stock Exchange.

The world's largest maker of construction and mining equipment and a closely watched component of the Dow Jones industrial average said fiscal stimulus programs, especially in China, were beginning to pay off and commodity prices were holding in a range that was positive for investment.

There is still a great deal of economic uncertainty in the world, Chairman and Chief Executive Jim Owens said in a statement. But we are seeing signs of stabilization ... Credit markets have improved significantly. Fiscal policy and monetary stimulus have been introduced around the world, and we are seeing signs, particularly in China, that they are beginning to work.

Caterpillar reported a second-quarter net profit of $371 million, or 60 cents a share, compared with $1.11 billion, or $1.74 a share, a year ago.

Revenue fell 41 percent to $7.98 billion.

Excluding costs associated with layoffs and restructuring, Caterpillar made 72 cents a share. Since the end of 2008, Caterpillar has cut 17,100 full-time workers.

Analysts, on average, had expected the Peoria, Illinois-based company to report a profit of 22 cents a share on sales of $8.36 billion, according to Reuters Estimates.

Caterpillar raised its outlook for full-year profit, including redundancy costs, to a range of 40 cents to $1.50 with a midpoint of 95 cents a share. When it reported first-quarter results three months ago, Caterpillar put that midpoint estimate at 50 cents a share.

Ann Duignan, an analyst at JP Morgan, said last quarter's better-than-expected results were driven higher margins in the company's engine business and a lower-than-expected tax rate of 10 percent.

Caterpillar's engines unit reported an operating profit of $555 million, down from $711 million last year.

In contrast, its machinery business, which makes tractors, excavators and other equipment, reported an operating loss of $252 million compared with an operating profit of $719 million a year ago, after lower sales in Europe, the Middle East, Africa and North America.

That unit's results suggest what rivals like Deere & Co , CNH Global NV , Terex Corp and Komatsu Ltd <6301.T> may report later this earnings season.

You have huge profitability in engines right now, said Eli Lustgarten, an analyst at Longbow Research. The question is how sustainable is that? That's what is saving the company right now.

During a conference call with investors to discuss its results, Caterpillar warned that its third quarter would be very tough on sales and said it was conceivable that it would lose money in that period.

Mike DeWalt, head of investor relations, said Caterpillar planned significant rolling plant shutdowns during the quarter to deal with the downturn, which he called extremely challenging.

Immediately after the call, Caterpillar released worldwide sales figures for the three months ended June 30.

Caterpillar said worldwide sales of its machines were down 47 percent in the three months ended June 30, compared with a decline of 43 percent in the three months ended May 31 and a decline of 39 percent in the three months that ended April 30.

Worldwide sales of engines were down 32 percent in the three months ended June 30, compared with a decline of 21 percent for the three months that ended May 31 and a decline of 9 percent for the three months ended April 30.

(Reporting by James Kelleher, Editing by Maureen Bavdek, Toni Reinhold)