Gov. Andrew Cuomo just saved the day for billionaire entrepreneur Elon Musk.

Cuomo on Friday said New York’s auto dealers have agreed to let Musk’s electric car company, Tesla Motors, keep its direct-selling stores open.

The deal was approved Thursday by both Tesla and several auto dealership groups across the state, sources said.

The agreement bans direct sales in New York but grandfathers in Tesla’s five existing stores.

“Today’s agreement reaffirms New York’s long-standing commitment to the dealer franchise system, while making sure New York remains a leader in spurring innovative businesses and encouraging zero emissions vehicle sales” Gov. Cuomo said in a statement.

The deal, which still needs to be approved by the state legislature, is the result of last-minute haggling that kicked off last week when a bill to ban Tesla’s direct- sales model met with near unanimous approval by a second committee in the state Assembly, sources told The Post.

That sent Tesla running to the bargaining table, where Cuomo’s top aide, Howard Glaser, acted as mediator, a source familiar with talks said.

Initially, the dealer groups, which backed the bill to shutter the five stores, softened their stance and wanted the stores to switch to a dealer model after eight years — but Tesla said no, sources said.

Dealers began their push to close the stores amid concerns that Tesla’s unique business model could pave the way for direct sales by auto makers from China and India. But it would have dealt a devastating blow to Musk, whose Tesla is currently the only auto maker in the state to sell its vehicles directly.

Tesla has met with similar bans or curbs on sales in Texas, Arizona, Virginia and Ohio. Earlier this month, it was also hit by a ban in New Jersey that will force Tesla to turn its two NJ stores into non-selling galleries.

Tesla got a two-week reprieve on Friday — until April 15.

“We are happy that the franchise system, which is good for the consumer, is being reinforced,” said Neale Kuperman, chairman of the Greater New York Automobile Dealers Association, one of the groups that backed the initial bill.

In New York, Musk had help in the form of powerful lobbyist David Weinraub, who was in charge of legislative and intergovernmental affairs for Cuomo’s dad, Mario, when he was governor, sources told The Post.

Weinraub, a partner at lobby firm Brown & Weinraub, didn’t return a call seeking comment.

The new legislation will be introduced with the support of Senate and Assembly leadership and therefore is expected to pass.

Separately, the National Highway Traffic Safety Administration on Friday closed an investigation into fires involving Tesla cars.

The decision by NHTSA helped spark a 2.4 percent jump in Tesla shares Friday, to $212.37.

The shares had fallen 18 percent in recent weeks.