Big gains in Amazon’s cloud-computing business helped push the company’s results into the black, when analysts were expecting red ink.

Share story shareholders have long been accustomed to the online retail giant investing the cash it brings in and posting the narrowest of profits or the more-than-occasional losses.

After a first-quarter loss and a profitable second quarter this year, Wall Street was poised for Amazon to return to its money-losing ways when it reported third-quarter results on Thursday. But Amazon surprised everyone.

The company had a profit of $79 million, or 17 cents a share, largely on the strength of Amazon Web Services, the unit that rents computing power and storage over the Internet to companies. The AWS division saw its revenue jump 78 percent to $2.1 billion in the quarter. Operating income for the unit more than quintupled to $521 million.

Not only did Amazon turn an unexpected profit; it posted higher-than-expected revenue as well. Net sales increased 23 percent to $25.36 billion. Analysts were expecting Amazon to lose 13 cents a share on sales of $24.91 billion.

The surprising results, which came after the stock market closed, left investors eager to buy Amazon shares in the after-hours market. The trading pushed the stock high enough to make Amazon founder and Chief Executive Jeff Bezos the third- wealthiest person in the United States, behind only Bill Gates and Warren Buffett, according to analysis by Bloomberg News. Bezos’ net worth now tops $55 billion.

Amazon had finished the trading day at $563.91, up $8.14, and then jumped $54.79 in after-hours trading to $618.70. If the shares open anywhere near that number on Friday, it will set an all-time high for the company’s stock.

The stock gain is an astonishing turn from a year ago, when Amazon stunned Wall Street with a larger-than-expected loss, which sent its stock price spiraling down below $300. Much of the blame back then was the disappointing performance of Amazon’s ill-fated Fire Phone, which has since disappeared from the market.

AWS has fueled Amazon’s bottom line for much of this year. Even as it grows into a business that could generate as much as $8 billion in annual sales, it also has become a moneymaking machine. Profit margins for the business, which hit 21.4 percent in the second quarter, expanded to 25 percent in the third quarter.

Also contributing to profits were advertising sales and black ink from third-party sellers on Amazon, a business known as Marketplace. They helped offset Amazon’s aggressive investments in developing businesses, such as streaming video, and in emerging markets, including India. That might lead to fewer quarters of red ink in the future, something Wall Street has long wanted for Amazon.

“They make most of their operating profit from AWS, Marketplace and Advertising; so as those businesses get larger, then … the overall mix becomes more profitable,” Robert W. Baird & Co. analyst Colin Sebastian said in an email interview.

Amazon also benefited in the quarter from Prime Day, a one-day worldwide sale in July that celebrated the company’s 20th anniversary. Amazon Chief Financial Officer Brian Olsavsky said in a conference call with journalists that Prime Day added 2 percent to the company’s revenue growth for the quarter, which amounts to roughly $460 million.

It also led to increased enrollment in Amazon Prime, the $99-a-year service that includes expedited shipping at no extra cost. That’s important because Prime members shop more often and spend more money than Amazon’s non-Prime customers.

“We will continue to add (Prime Day) … in 2016 and beyond,” Olsavsky said.

Shareholders also bid up the stock on Amazon’s forecast of a profit and sales jump in the fourth quarter, which includes the crucial holiday season. The company expects operating income of $80 million to $1.28 billion, compared with $591 million in operating profit in the fourth quarter of 2014. The company expects net sales to climb 14 percent to 25 percent to $33.5 billion to $36.75 billion.

The profits come despite Amazon’s continued hiring and spending on operations. The company now has 123 warehouses worldwide, 14 more than it had at the beginning of the year. It also has 23 sortation centers, where the company sorts packages for delivery to carriers, up from 15 at the end of last year.

The growth of those buildings has ballooned Amazon’s head count. The company now employs 222,400 workers, a 21 percent jump from the previous quarter.

Amazon also has doubled the number of Kiva robots, which automate the picking of products from shelves at the company’s warehouses, since the end of last year. It now has 30,000 robots deployed at 13 of its warehouses.

Olsavsky cautioned analysts to expect “lumpy” spending from quarter to quarter as the company ramps up new initiatives and pumps money into key businesses.

“There’s a lot of investment going on, and there will (continue to) be,” Olsavsky said.

This story was originally published Oct. 22, 2015, and corrected the next day. The chart originally gave Amazon profit and loss numbers in billions; those numbers were actually millions.