LONDON— Shell PLC said it took a $3.9 billion posttax charge related to its decision to exit Russia, only slightly denting an otherwise strong quarter bolstered by soaring commodity prices.

The charge was expected and came alongside robust oil and gas trading profit during a period of extreme volatility. Shell’s first-quarter profit on a net current-cost-of-supplies basis—a figure similar to the net income that U.S. oil companies report—was $5 billion, compared with $4.3 billion a year earlier hen performance rebounded from low pandemic energy demands.

Soaring oil and gas prices have showered the biggest producers with cash, which they are largely using to reduce debt, accelerate share buybacks and otherwise reward investors, rather than increase exploration and other capital spending.

Shell said Thursday it had completed $4 billion of $8.5 billion in share buybacks it plans through the first half of this year. Its adjusted earnings stripping out certain commodity-price adjustments and one-time charges beat analysts’ consensus forecast for the quarter. Shares rose more than 3% in early trading.

The company is the latest energy giant to document a financial hit from exiting Russia following its invasion of Ukraine. Earlier this week, London-based rival BP PLC took a $25.5 billion pretax accounting charge related to its decision to exit its Russia holdings, including its stake in government-controlled Rosneft. The charge—the biggest tallied by Western companies pulling out of Russia—dragged BP into a $20.4 billion headline loss for the first quarter. Aside from the charge, BP’s first-quarter earnings beat analyst expectations.

Shell in early April said it planned to take accounting charges of between $4 billion and $5 billion as a result of exiting Russia, including terminating joint ventures with energy giant Gazprom PJSC. That followed the company’s February announcement that it would end collaborations, including financing the halted Nord Stream 2 natural-gas pipeline project between Russia and Germany.

Shell also said at the time it would exit its 27.5% stake in a major offshore-gas project in Russia’s Far East that is 50% owned by Gazprom and supplies around 4% of the world’s liquefied-natural-gas market.

In early March, Shell Chief Executive Ben van Beurden apologized for the company’s purchase of a cargo of Russian crude at a bargain price, saying the energy giant would phase out Russian oil purchases but couldn’t do so overnight.

Shell said Thursday it has stopped all spot purchases of Russian crude but is “still legally obliged to take delivery of crude bought under contracts that were signed before the invasion.” It said most of its long-term contracts to buy Russian crude will end this year, but added that it still has “long-term contractual commitments” for Russian liquefied natural gas.

The company’s hurdles in phasing out Russian energy purchases reflect the complexities of Europe’s attempts to sever its reliance on the country’s resources.

“Reducing European reliance on piped natural gas supplies from Russia is also a very complex challenge that requires concerted action by governments, as well as energy suppliers and customers,” Shell said in its earnings statement.

Shell’s results reflect a broader comeback from lagging energy consumption earlier in the Covid-19 pandemic that led major oil companies, including Shell, to slash dividends. Last year, Shell reconfigured its operations, consolidating its dual British-Dutch structure, moving its headquarters to London from The Hague and dropping “Royal Dutch” from its name amid a focus on lower-carbon businesses as part of the energy transition.

Shell for the first time broke out profit from its renewable-energy business, rather than wrapping them into its integrated gas division. The change reflects investors’ growing emphasis on lower-carbon energy and interest in how well companies like Shell, long dependent on oil and gas production, can convert their businesses to profit in a new era.

Shell’s renewable-energy unit contributed $344 million to the company’s overall $9.1 billion in adjusted earnings in the first quarter, compared with $4.1 billion in its largest division, integrated gas.

The biggest chunk of Shell’s financial hit from exiting Russia was tied to the Sakhalin 2 integrated oil and gas project in the country’s Far East. Shell wrote down $1.6 billion tied to that asset alone, reflecting its loss of managerial influence and profit tied to the massive project. Another $1.1 billion piece of the Russia-related charges was tied to Shell’s involvement in financing Nord Stream 2.

France’s TotalEnergies SE, which unlike most European rivals hasn’t said it is fully exiting its Russian operation, last week took a $4.1 billion accounting charge on the value of its natural-gas reserves. Total cited impacts from Western sanctions targeting Russia on a massive Arctic liquefied-natural-gas project under development called Arctic LNG 2.

Also last week Exxon Mobil Corp. said it took a $3.4 billion accounting charge after it decided to halt operations at its Sakhalin Island development in Russia.

Write to Jenny Strasburg at jenny.strasburg@wsj.com