ALBANY—Gov. Andrew Cuomo and legislative leaders on Thursday agreed to legalize certain forms of marijuana for patients with a limited range of diseases, capping two decades of efforts around a polarizing national issue.

The deal came during a flurry of last-minute negotiations in the Capitol, as lawmakers and the governor worked to finalize bills before the Legislature goes home for the year. The Senate and the Assembly both passed the bill letting New York City lower its default speed limit to 25 miles an hour. It now needs to be signed by the governor to become law.

After days of intense negotiations, the medical pot bill was more limited than many lawmakers wanted, after the governor warned he would sign it only with strict requirements.

It would permit only doctors to prescribe marijuana, in forms including oil-based and vapor, to individuals with any of about a half-dozen conditions, including cancer, AIDS, epilepsy and multiple sclerosis.

But it wouldn't legalize smokable forms of the drug, making the effort much narrower in scope than many other medical-marijuana laws around the country. And it would allow the governor, upon recommendation by the state police superintendent or health commissioner, to suspend the program at any time.