Electric-car maker Tesla Inc. TSLA -3.46% has reached an agreement to set up its own manufacturing facility in Shanghai, according to people briefed on the plan, a move that could help the company gain traction in China’s fast-growing EV market.

The deal with Shanghai’s government will allow the Silicon Valley auto maker to build a wholly owned factory in the city’s free-trade zone, these people said. This arrangement, the first of its kind for a foreign auto maker, could enable Tesla to slash production costs, but it would still likely incur China’s 25% import tariff.

Tesla is currently working with the Shanghai government about details of the deal’s announcement, such as timing, one of these people said. The effort comes as President Donald Trump, who has been critical of China’s trade policies, prepares to visit Beijing early next month.

A Tesla spokesman didn’t have a comment beyond reiterating the company’s previous statement in June that it planned to “clearly define” production plans in China by year’s end. The Shanghai government didn’t reply to a request for comment.

China’s electric-vehicle market—already the world’s largest—is primed for growth. The Chinese government is targeting seven million EV sales a year by 2025, up from 351,000 last year, and in September it ordered all auto makers already operating in China to start producing EVs by 2019. Officials have also said they are working on a plan to ban gasoline cars.

Accelerating Growth / Forecasts for Tesla sales are high as it aims to ramp up output of the more affordable Model 3. Number of Tesla vehicles delivered*: Source: Factset

China had previously circulated a proposal that would allow electric-car makers into the country without local partners if they were to locate in the so-called free-trade zones. The government set up the country’s first such zone in Shanghai in 2013, and has since approved 10 more around the country.

Until now, foreign auto makers have built cars in China through joint ventures with local manufacturers. That allows them to avoid the 25% tariff on autos, but also forces them to split profits, and potentially share technology, with the local partner—something that has tripped up Tesla’s previous efforts to expand there.

Under current rules, the cars Tesla builds in the free-trade zone would still count as imports and incur the tariff. Auto analysts in Shanghai doubt the Chinese government has any incentive to give Tesla special treatment.

“Government regulators examine every deal and try not to set a precedent,” said Bill Russo, chief executive of Automobility, a Shanghai-based consultancy, and a former Chrysler executive. “Whatever deal Tesla gets, others will want it too.”

A plant in Shanghai’s free-trade zone still has clear benefits, Mr. Russo said. It would give Tesla a base from which to export to the region, while offering proximity to the Chinese supply chain, thereby lowering production costs and the sale price of Tesla cars sold there. Today, a Tesla costs roughly 50% more in China than it does in the U.S.

Uphill Road / Tesla's sales in China as a percent of total sales Source: S&P Global Intelligence

Manufacturing in Shanghai would also put Tesla in good standing with the Chinese government, said Michael Dunne, an auto-industry consultant who spent years in Asia. Having Tesla cars built on Chinese soil would please Beijing officials, he said, which “in turn, will give Tesla goodwill leverage to negotiate better China market-access terms in the future.”

The auto maker reported more than $1 billion in revenue in China for 2016 on sales of roughly 11,000 imported vehicles, representing about 15% of total revenue. Sales in China were up from about $319 million in 2015.

In June, Tesla revealed it was in talks with the Shanghai government about the possibility of opening a factory and reiterated that it aims to define its China production plans by year’s end. A month earlier, Chief Executive Elon Musk cryptically had told analysts that a change in China rules would be “good timing.”

Elon Musk, Tesla’s chief executive, spoke at a forum in Hong Kong in January 2016. Mr. Musk has previously signaled a desire to expand Tesla’s manufacturing capabilities in China and Europe. Photo: Justin Chin/Bloomberg News

Mr. Musk has said Tesla could cut prices in China by one-third by reducing shipping costs and avoiding import duties.

Mr. Musk has previously signaled a desire to expand manufacturing capabilities in China and Europe. The company, which manufactures its vehicles in Fremont, Calif., does final assembly at a facility in Tilburg, Netherlands, for the European market.

Fremont is currently under pressure to expand manufacturing capacity to meet Mr. Musk’s ambitious goals of making 10,000 Model 3 sedans a week by the end of next year. The Model 3, priced starting at $35,000, is part of his vision for expanding the auto maker beyond selling luxury niche vehicles.

While the cost of introducing the new vehicle has left the auto maker with little cash to spare, investors’ enthusiasm for Mr. Musk’s vision has helped push shares of the company up more than 50% this year so far, propelling Tesla’s market value to rival General Motors Co.’s.

Chinese internet company Tencent Holdings Ltd. acquired a 5% stake in Tesla in March, giving Mr. Musk a powerful ally in China.

—Yang Jie in Beijing contributed to this article.

Write to Tim Higgins at Tim.Higgins@WSJ.com, Trefor Moss at Trefor.Moss@wsj.com and Eva Dou at eva.dou@wsj.com