(New York) CNN Money — 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.
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.
Yet CEO Tim 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.
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.
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.