Apple on Tuesday reported strong earnings on solid iPhone sales, and it surprised investors by announcing that it would buy back $100 billion of its own stock.

Those two forces pushed up Apple shares by 4 percent after hours.

Apple CEO Tim Cook credited a 2017 cut in federal tax rates for enabling the company to return more money to shareholders. "Tax reform made it possible for us to execute our program... more efficiently," he said.

Get Breaking News Delivered to Your Inbox

Analysts had been worried that sales in of its iPhone had been weak, especially the newest iPhone X.

But Apple sold 52.2 million iPhones in the second quarter, compared with 50.8 million a year ago. iPhone revenue came in at $38 billion, a 14 percent increase from the same quarter last year. Overall company revenue grew 16 percent, to $61 billion.

Analysts had also been worried about Asia, with mounting competition from Chinese phone makers. But Apple's revenue in China grew 21 percent.

Cook said sales in China were driven by several factors, including sales of the iPhone X, the services segment and wearables. Cook said that despite recent trade tensions between the U.S. and China, he's optimistic.

"My own view is that China and the U.S. have this unavoidable mutuality where China only wins if the U.S. wins and the U.S. only wins if China wins," Cook said."So I'm a big believer that the two countries together can both win and grow the pie, not just allocate it differently and so that's our focus...over time I think that view will prevail."

Investors have been on edge amid signs of disappointing iPhone sales, including Apple's flagship iPhone X model. Wall Street has been concerned that the technology giant's bet on the high-ticket device may not pay off and that consumers aren't embracing other products like Apple AirPods, Apple Music and the Apple Watch.

The iPhone X carries a starting price tag of $999, while two other recently released phones -- the iPhone 8 and iPhone 8 Plus -- start at $699 and $799, respectively.

"Longer term, we think Apple is struggling with the rising cost of adding new features, necessitating higher iPhone prices," analysts at Raymond James said in a note last month, adding that sales of the iPhone X so far "doesn't leave us encouraged."