Warren Buffett’s Berkshire Hathaway Inc. scooped up millions more shares of Apple Inc. and doubled down on its energy investments, while the stock market swooned in the second quarter.

The moves were made public in Berkshire’s 13F filing, which was released after the stock market closed Monday. Regulations require institutional investors managing more than $100 million to file the form, which lays out firms’ equity holdings as of the end of the most recent quarter, as well as the size and market value of their positions. 

The filing showed Berkshire bought 3.9 million shares of Apple, with its stake worth $125 billion as of the end of June. Apple makes up roughly 40% of Berkshire’s stock portfolio.

Berkshire also purchased 22 million shares of Occidental Petroleum Corp. and 2.3 million shares of Chevron Corp. in the second quarter, adding to its big bet this year on U.S. oil. It added to its stakes in Ally Financial Inc. and Paramount Global, among other companies.

Meanwhile, Berkshire closed out its stakes in Verizon Communications Inc. and Royalty Pharma PLC and trimmed its holdings of General Motors Co. , U.S. Bancorp and Kroger Co. It didn’t open any new positions.

Investors closely watch what Berkshire is buying and selling because of Mr. Buffett’s reputation as one of the most successful investors of all time. While Mr. Buffett doesn’t personally pick all of the company’s stock investments—Berkshire’s decision to begin buying shares of Apple in 2016, for instance, was made by deputies Todd Combs and Ted Weschler—investors nevertheless like to draw as many insights as they can from Berkshire’s filings.

Berkshire’s five biggest stockholdings as of the end of June were Apple, Bank of America Corp. , Coca-Cola Co. , Chevron and American Express Co. The five stocks made up about 69% of Berkshire’s portfolio, according to company filings.

Berkshire had already disclosed that it bought roughly $6.2 billion of stocks in the second quarter. That was down from a whopping $51 billion in the first quarter but nevertheless extended a recent buying streak for the company.

It’s a stark change from last year. The Omaha, Neb.-based Berkshire, which includes railroad, energy and insurance businesses, as well as a huge investment portfolio, largely stuck to repurchasing its own shares in 2021. As recently as February, Mr. Buffett had lamented a lack of good buying opportunities as a way of explaining why Berkshire was mostly investing in its own stock. 

But in the months since, market volatility has shot up. That has given Berkshire a chance to put its cash to work—especially within the energy sector. Berkshire snapped up shares of Occidental Petroleum as Russia’s invasion of Ukraine sent oil prices surging.

Investors and analysts have said Berkshire’s buying potentially reflects a view from Mr. Buffett that inflation in energy prices will remain elevated for some time. They have also noted that Mr. Buffett has expressed admiration of Occidental’s commitment to paying down debt, issuing dividends and buying back Occidental’s stock.

Energy stocks have been by far the best performers in the market this year. Occidental is up more than 110% for the year, while the S&P 500 is down 9.8%. U.S. crude was up 19% for the year through Friday but down 28% from its March high.

Write to Akane Otani at akane.otani@wsj.com