New York state lawmakers have struck a deal to legalize electric bicycles and scooters in the closing days of the legislative session, a move that sounds the death knell for the city’s controversial prohibition on the devices.

The deal, announced Monday, emerged from negotiations between state lawmakers and city officials that went into Sunday night, a person familiar with the talks said.

The bill removes state-level restrictions against motorized scooters and bicycles that can be propelled exclusively by electric motors – but retains local jurisdictions’ power to ban them. Neither type of vehicle will be street legal in New York City itself until the city eliminates its own ordinance prohibiting their use.

Both Mayor de Blasio and City Council Speaker Corey Johnson had declined to entertain such a change until the state took action.

“We appreciate this common-sense legislation that clarifies the rules around e-bikes on our streets,” said City Hall spokesman Seth Stein. “Safety for everyone on our roads is our priority, and we look forward to working with legislators and communities as we develop plans to implement the new law.”

Existing state and city laws prohibit the use of both e-bikes and e-scooters, even though they’re legal to purchase. Last year, city officials legalized the use of pedal-assisted e-bikes, but $500 fines remain in place for the throttle-powered bikes primarily used by food delivery workers.

Under the new state rules, e-bikes and e-scooters would barred from sidewalks unless local officials say otherwise.

The legislation has other caveats: E-bikers and e-scooter riders must be at least 16 years old. Vehicles are capped at 20 miles per hour. Both e-scooters and e-bikes will be banned from the Hudson River Greenway.

And local governments would have total control over any scooter “share” companies launching within their jurisdictions.

But a provision aimed to prohibit scooter share companies from operating in Manhattan appears to have misstated the borough’s population – making it effectively moot.

Manhattan legislators, whose districts are hot spots for e-bike complaints, reportedly killed a similar bill during budget negotiations earlier this year.

For e-bikes, the law would take effect 180 days after passage and for e-scooters, 270 days. But it would allow localities to permit the vehicles on their own accord at any time.

“This bill is a giant step forward,” said Transportation Alternatives Co-Executive Director Marco Conner. “It removes any doubt that New York City can act immediately to legalize.”