TOKYO: Toyota Motor Corp.’s decision to build a stockpile of chips for its cars paid off by lifting it above perennial top dog General Motors Co. in the U.S. for the first time.

But the Japanese car maker, whose American dealers have supply problems of their own, isn’t thumping its chest about its triumph over Detroit.

Between April and June, Toyota sold 688,813 vehicles in the U.S., giving it a razor-thin 577-unit margin of victory over GM, according to figures from the two companies. It was the first time a Japanese car maker took the top position in the U.S., according to car-shopping website, and came as the politically sensitive U.S. trade deficit is widening.

“Toyota thinks this was an unusual case due to production constraints and other factors,” spokeswoman Shino Yamada said. She called it a “short-term event for this quarter.”

Toyota bet earlier than most car makers on a recovering U.S. car market. As a result, the company cut production and parts orders less sharply than competitors, making it better prepared for the current surge. But even Toyota wasn’t fully prepared for the frenzy of car buying.

During the post-pandemic boom in the U.S., auto dealers’ problem isn’t finding customers—it is finding cars to sell them. Factories have suffered from a shortage of parts, in particular the semiconductors that go into everything from engines to key fobs.

That is where Toyota has had an edge this year.

Building on its experience following Japan’s 2011 earthquake, Toyota eased away from a strict application of its just-in-time production system, in which parts are delivered to factories right as they are needed. It has said it built up a four-month stockpile of chips and other key parts.

While other car makers were shutting down factories because of the shortages, Toyota was nearly unaffected, according to research firm LMC Automotive. Toyota’s factories have run at over 90% capacity so far this year, compared with 50% to 60% for most of its rivals, according to LMC data.

Toyota dealers are still dealing with a severe shortage of cars on their lots, but they are somewhat better supplied than the competition with models like the RAV4 sport-utility vehicle, Toyota’s bestseller in the U.S.

Stephen Wade owns several dealerships in Utah, including ones that sell Toyotas and Chevrolets. “My GM store really got hurt. It looks like a war zone, like I’m going out of business,” he said. “I don’t have a lot of RAV4s, but they’re trickling in.”

There are signs that customers are looking to Toyota when they can’t find what they want elsewhere.

“While we are low-inventory on trucks and SUVs, we are seeing some competitors’ customers at our dealerships,” said Victor Vanov, a U.S.-based spokesman for Toyota.

The market is so tight that sedans are selling again, Toyota said, after years in which the U.S. tilted toward trucks and SUVs. Dealers said the current sales frenzy includes people who can’t afford one of the bigger models.

“Not everyone can spend 60 grand on a truck,” said Adam Lee, chairman of Lee Auto Malls, which owns 19 dealerships in Maine including a Toyota and GMC dealership.

Most Toyotas sold in the U.S. are manufactured at the company’s North American factories in Kentucky, Indiana, Texas and elsewhere. But some, including most high-end Lexus models, are exported from Japan. Toyota’s exports to the U.S., which predominantly come from Japan, rose 16% to 234,229 units in the first five months of this year.

The U.S. trade deficit is widening again this year. So far, the Biden administration hasn’t trained its fire on allies that have trade imbalances with the U.S., but Tokyo remains wary after getting hit with tariffs on steel during the Trump administration.

Toyota’s ability to stay ahead in the production race will likely determine whether it remains No. 1 in the second half of this year. It said its plants are operating normally now and declined to comment on future semiconductor supplies.

Other companies say the shortage isn’t getting better soon.

Ford Motor Co. is cutting or stopping production at several of its factories this month. Mazda Motor Corp. said it would have to shut down production at its plant in Hofu, Japan, for 10 days this month, potentially crimping supplies of its Mazda 3 sedan and CX3 sport-utility vehicle.

Subaru Corp. has struggled to keep production up all year and had only nine days’ worth of inventory as of the end of May compared with 45 days in a normal year, the company said.

“We are eating into our reserves to sell our products,” a Subaru spokeswoman said.

On Tuesday, the website for Longo Toyota, the world’s largest Toyota dealership, showed it had a single RAV4 in stock.

Mr. Lee of the Maine dealership chain said Toyota has barely managed to keep him supplied with enough vehicles. He said he had only 17 vehicles on the lot at his Toyota dealership, around 10% of the normal number.

“They come in and they get sold right away,” he said.