Recreational use will remain illegal.
Marijuana was used in Thailand as a traditional medicine, until it was banned in the 1930s.
South East Asia has some of the world's toughest penalties for drug usage or possession, and Thailand is the first in the region to allow medicinal marijuana.
Thailand's junta-appointed parliament voted to amend the Narcotic Act of 1979 on Tuesday.
It happened after an extra parliamentary session was arranged to push bills through before the New Year's holidays, according to Reuters news agency.
The amendment will become law when it is published in the government gazette, The Bangkok Post reported.
"This is a New Year's gift from the National Legislative Assembly to the government and the Thai people," said Somchai Sawangkarn, chairman of the drafting committee, during the televised session.
Consumers will be able to carry specified amounts necessary for medicinal purposes, if they have a prescription or recognised certificate, the Bangkok Post said.
Licences for production and sale of the product will be strictly controlled.
The paper said the law also applies to kratom, a South East Asian plant that acts as a stimulant.
Across the world, countries have been revisiting their marijuana laws.
Canada and Uruguay are among those to have legalised it, including for recreational use. However South East Asia is known for having very harsh punishments for drugs-related charges.
Earlier this year, a man in Malaysia was given a death sentence for selling cannabis oil.
Meanwhile, on the Indonesian island of Bali, a British man is facing up to 15 years in prison after being found with cannabis oil which he says he needed for medical reasons.
The 45-year-old Pip Holmes, from Cornwall, says he asked a friend to send it to him while he was living in Bali to help his arthritis.