Proponents said it is a major step that will address Mexicans' need of an alternative medical treatment.
In a major policy shift, President Enrique Pena Nieto proposed legalizing medical marijuana in April after his government organized forums to discuss changes to the laws.
Senators voted 98-7 for the legislation, moving Mexico closer to joining several US states and other nations in Latin America in allowing cannabis for medical uses.
The bill -- which now moves to the lower chamber of deputies -- falls short of demands among some lawmakers and civil groups who argue that a wider legalization of marijuana use can help the country reduce drug-related violence.
But proponents said it is a major step that will address Mexicans' need of an alternative medical treatment.
The bill authorizes the health ministry to design regulations for the use, import and production of pharmaceutical products made from cannabis or marijuana, including tetrahydrocannabinol (THC), the plant's main psychoactive ingredient. Products with one percent concentration of THC would be allowed.
The cultivation of marijuana for medical and scientific purposes will not be punishable.
Senator Cristina Diaz, of Pena Nieto's centrist Institutional Revolutionary Party (PRI), said the legislation was a historic move that will "lay the foundation to establish in our country an industry for the national production of medicine with cannabis."
Senator Angelica de la Pena Gomez, of the leftist Democratic Revolution Party (PRD), said there was consensus to "do something different in drug policy" because prohibition "has generated high levels of violence, more than 100,000 deaths and the systematic violation of human rights."
But Senator Miguel Barbosa, of the PRD, lamented that the legislation was "well below the expectations of society" regarding reforms of marijuana laws.
Senator Armando Rios Piter voted against what he called a "tiny" step to resolve a "failed policy in the battle against organized crime" and called for a discussion on regulating opium poppy production, a source of violence in Guerrero state.
A family in northern Mexico became a symbol of the push to legalize medical marijuana last year when the parents of a young epileptic girl won a court battle to import a cannabis-based treatment to stop her daily seizures.
The girl's father, Raul Elizalde, told AFP that the legislation represented "great progress" but that it should make it easier for patients to acquire THC by letting them buy it without a prescription.
In a separate major case in November 2015, the Supreme Court authorized four individuals to grow and smoke pot for recreational purposes.
Pena Nieto is opposed to a broader legalization of marijuana but he has proposed increasing the amount of the drug that can legally be possessed for personal consumption from 28 grams (one ounce) from five grams.
The Senate will draft separate legislation on raising the possession limit.
For Elizalde, a broader legalization of marijuana is "still a long way off" in Mexico.