Electric-car maker Tesla Inc. 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...