The legislation also allows use of marijuana for scientific research.
Mexican lawmakers gave final and overwhelming approval on Friday to a bill legalising medical marijuana after a national debate on narcotics policy in a country scarred by brutal drug violence.
The legislation also allows use of marijuana for scientific research, as well as its production and distribution for those two stated purposes.
The vote in the Chamber of Deputies (Lower House of Congress) was 371 in favor and seven against with 11 abstentions. The bill will now go to President Enrique Pena Nieto for his signature.
The Senate approved the bill by a wide margin in December.
With Friday's vote, Mexico will join other nations in Latin America and several US states that allow cannabis for medical uses.
Pena Nieto proposed legalising medical marijuana in a major policy shift in April after his government organised forums to discuss changes to the laws.
The bill fell short of demands from some lawmakers and civil groups that argue that a wider legalisation of marijuana use could help the country reduce drug-related violence.
But proponents said it was a major step that will address Mexicans' need of an alternative medical treatment.
The bill authorises 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 will be allowed.
Growing marijuana for medical and scientific purposes will not be punishable.
A family in northern Mexico became a symbol of the push to legalise 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, said then 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 authorised four people to grow and smoke pot for recreational purposes.
Although Pena Nieto is opposed to a broader legalization of marijuana, he has proposed increasing the amount of the drug that can legally be possessed for personal consumption.