Apple reported earnings for its June quarter on Tuesday that were above expectations, and the company's revenue returned to growth after two straight down quarters.

The stock rose more than 4% in after-hours trading.

Apple's guidance was strong and beat analyst expectations, suggesting that demand for Apple products is stabilizing headed into the critical second half of the year. Apple also declared a cash dividend of $0.77 per share.

Here's how the company did versus what analysts were expecting:

EPS: $2.18 vs. $2.10 estimated by Refinitiv consensus estimates.

$2.18 vs. $2.10 estimated by Refinitiv consensus estimates. Revenue: $53.8 billion vs. $53.39B estimated by Refinitiv consensus estimates.

$53.8 billion vs. $53.39B estimated by Refinitiv consensus estimates. Q4 revenue guidance: $61 billion to $64 billion versus $60.98 billion estimate by Refinitiv consensus estimates.

$61 billion to $64 billion versus $60.98 billion estimate by Refinitiv consensus estimates. iPhone revenue : $25.99 billion vs. $26.31 billion estimated by FactSet.

: $25.99 billion vs. $26.31 billion estimated by FactSet. Services revenue: $11.46 billion vs. $11.61 billion estimated by FactSet.

"We're very excited to report a return to growth for the quarter, and it's a record revenue for Q3 as well, best we've ever had," Apple CEO Tim Cook told CNBC's Josh Lipton.

Apple's revenue was up 1% from the year-ago quarter. Earnings per share were down 7%.

"Great services quarter, unbelievable wearables quarter, significant progress on iPhone, and off-the-charts significant progress on China, compared to where we were the previous quarter," Cook continued.

Apple said that it had spent over $17 billion on share buybacks of almost 88 million Apple shares, and had also paid out $3.6 billion in dividends and equivalents during the quarter.

"The highlight of this release and focus of investors in our opinion will be the robust September guidance for total revenue of between $61 billion and $64 billion vs. the Street's $60.9 billion estimate," Wedbush analyst Dan Ives said in a note.