NEW YORK (CNNMoney) -- As it turns out, Apple still has a little surprise left in it.
The company sold 31.2 million iPhones last quarter -- a number practically no one saw coming -- lifting Apple's overall sales and helping Apple's profit surpass some pretty gloomy expectations from Wall Street analysts.
That's not to say Apple had a great past quarter. Profit sank 22% and sales were up less than 1%. That's a nasty development for a company that was regularly posting startup-like profit and sales growth as recently as a year ago. But no one expected Apple to report anything close to that last quarter.
Shares of Apple rose 4% after hours, as investors reveled in a rare bit of good news for the company. Apple's stock has taken a beating over the past 10 months, falling 40% since reaching an alll-time high in September.
The company's iPhone sales, up 20% from a year ago, easily beat Wall Street's expectations of about 26 million. CEO Tim Cook said he believes this past quarter is evidence that the obituaries written for growth in the high-end smartphone market are premature. Cook said the latest-edition iPhone 5 remains the best-selling iPhone "by far."
Another unexpected bright note: Apple's iTunes sales grew by 29% last quarter -- a business that now accounts for nearly 7% of Apple's overall sales.
But Apple sold just 14.6 million iPads, down 14% from a year ago, when the company released the third-generation iPad. It also sold 3.8 million Macs, down 5% from a year earlier, though the overall PC market contracted by about twice that rate. Still, Mac sales fell short of analysts' forecasts -- and iPad badly underperformed expectations.
The company also said its current quarter won't be as strong as Wall Street had expected. Apple anticipates producing sales of between $34 billion and $37 billion this quarter, below analysts' median forecast of just over $37 billion.
The growing mix of older and cheaper products, including the iPhone 4 and iPad mini, continue to weigh on the company's profit. Apple said it expects gross margin to come in between 36% and 37% this quarter. If that holds true, this would mark the seventh straight quarter in which gross margins have fallen.
Yet Cook said he was encouraged by the company's better-than-expected quarter, and he continued his optimistic view of the company's future.
"We are laser-focused and working hard on some amazing new products that we will introduce in the fall and across 2014," he said, in a prepared statement. Peter Oppenheimer, Apple's chief financial officer, said the company will have "a very busy fall," though he declined to elaborate.
By the numbers: The tech giant said net income in its fiscal third quarter fell to $6.9 billion, or $7.47 per share. Analysts polled by Thomson Reuters forecast earnings of $7.32 per share. Apple's gross margin of 36.9% also came in slightly ahead of analysts' expectations, but that fell from 42.8% a year ago.
Sales for the Cupertino, Calif.-based company rose to $35.3 billion, topping analysts' forecasts of $35 billion.
Apple ended the quarter with $146.6 billion in cash -- $106 billion of which is being held overseas.
™ & © 2013 Cable News Network, Inc., a Time Warner Company. All rights reserved.