Image copyright Getty Images

Online giant Amazon's shares have closed 9.8% higher after it posted an unexpected profit on Thursday.

The online retailer reported a $92m profit for the three months to 30 June, surprising investors who had expected another loss. Sales rose by 20% to almost $23.2bn.

The results sent shares in Amazon up 9.8% to $529.42, making it worth $246.5bn.

The company's stock has risen by 78% since the start of the year.

Amazon has not often reported a profit and last did so for the fourth quarter of 2014.

Instead, the 20-year old Seattle-based company has reinvested profits in a bid to improve the delivery infrastructure at the heart of its business.

Analysts said the results showed the firm's investment in its own business was finally beginning to pay off.

'Magical trend'

Revenue from its cloud operations - Amazon Web Services - nearly doubled in the second quarter, which analysts said showed the firm was now well positioned to grow.

Deutsche Bank analyst Ross Sandler said the fact it was increasing revenue and margins - the amount of money it makes after costs - was a "magical trend" that was being "rightfully rewarded".

"After questioning the investment rationale for years, Amazon is seeing the fruits of its labours in both revenue growth and operating margin," he wrote in a note to clients.

Barclays analyst Paul Vogel said it was now optimistic rather than cautious on the company's growth prospects.

"The scale of their distribution network is starting to generate better incremental margins," he added.

Amazon chief financial officer Brian Olsavsky told investors that the trend would continue as the company would continue to invest in "things that we think are big and important" while keeping costs under control.