By Jonathan Cheng

BEIJING--China's export machine gained steam in August as countries gradually recovering from coronavirus lockdowns, including the U.S., snapped up more Chinese-made goods.

Outbound shipments from China rose 9.5% in August from a year earlier, beating July's 7.2% increase and economists' median forecast for 7.3% growth, data released by the General Administration of Customs showed Monday.

August marked the third straight month that China's exports outpaced those of the same month last year. It marks a stark turnaround from the start of the year, when the pandemic crippled China's factories and global shipping networks.

In the depths of the coronavirus pandemic, economists had predicted the export sector would be among the hardest-hit in China's economy, as countries shut down their borders and global demand dropped.

Instead, Chinese exporters, aided by the government's heavy-handed but effective control of the coronavirus and its policies to support the economy, managed to grab a greater share of global exports in the second quarter while other exporting countries remained paralyzed by the pandemic.

Key to the export recovery has been China's outbound shipments of coronavirus-related medical supplies, including face masks, though economists expect the momentum to slow in the coming months as other countries resume production. Electronic goods have also fared well as swaths of the developed world shifted to online work, said Xing Zhaopeng, an economist with ANZ, an investment bank.

Beyond those two areas of strength, however, other export categories continued to be just lukewarm, which could mean China's outperformance on exports could fade, Mr. Xing said.

In the coming months, China's exporters are likely to face increased competition from countries such as Vietnam and South Korea, some of which have overcome the worst of the pandemic and are quickly ramping up production capacity again, says Ning Zhang, an economist at UBS.

"For low- and middle-end products, these exporters may gradually regain market share" from China, Mr. Zhang said.

China's imports, meantime, fell 2.1% in August from a year earlier, worse than July's 1.4% year-over-year drop and economists' expectations for a 0.2% fall. The steep drop-off in imports was mainly due to low commodity prices and a high-base comparison from the previous year, said Julian Evans-Pritchard, an economist at Capital Economics.

"Imports are doing just as well as exports once price effects are adjusted for," Mr. Evans-Pritchard said. He estimated that by volume, imports actually rose by 9.5% in August from last year.

China has stepped up purchases of commodities in global markets thanks to increased infrastructure investment at home and its trade-deal commitment to buy more U.S. energy and farm products.

China's imports from the U.S. rose 1.8% in August from a year earlier in dollar terms, slower than July's 3.6% increase.

However, Chinese exports to the U.S. jumped by 20% in August, up from 12.6% in July. That is partly due to the sudden drop-off in exports the previous year as the U.S.-China trade war intensified, said Mr. Zhang of UBS.

The increased flow of Chinese goods to the U.S. in August widened the trade gap between the world's two largest economies to $34.24 billion, the largest margin in almost two years.

In a videoconference call last month, senior U.S. and Chinese trade officials said they were committed to carrying out the two countries' phase-one trade deal, which was signed in January. The two sides also discussed "significant increases" in the purchase of U.S. products by China.

China has increased its purchases of U.S. corn, soybeans and other farm products in the past few months, but the pace and the value of purchases have fallen far short of what would be needed to meet the targets, largely due to the plunge in commodity prices after the coronavirus pandemic.

It is unlikely that Beijing will be able to realize the purchasing targets it had set at the beginning of the year, said Mr. Xing of ANZ, though he added that he expected the Trump administration to accept it as long as China keeps increasing its purchases.

Grace Zhu and Bingyan Wang contributed to this article.

Write to Jonathan Cheng at jonathan.cheng@wsj.com