SEOUL—Dog meat will no longer be allowed in South Korea, capping a decadeslong campaign against a controversial practice that many locals have come to view with unease.

With no dissenting vote, lawmakers passed a bill Tuesday that will phase out the farming, slaughter and sale of dog meat by 2027. Violators will face a prison sentence of up to three years or a fine of roughly $23,000.

The three-year grace period was granted following a backlash by South Korea’s dog-meat industry, which argues the ban threatens their economic livelihoods and violates their human rights. Dozens of dog-meat farmers in recent months have clashed with law enforcement at protests. Some have threatened to release their unsold stock of dogs in public areas—including near South Korea’s presidential complex.

Restaurants, breeders and middlemen must register with their local government. Some compensation will be provided during the phaseout period, though the amount has yet to be decided.

South Korea has racked up global soft power over the years, with glitzy pop stars, innovative technology and trendy food. But for decades, eating dogs, to many South Koreans, has represented a societal sore—and a punchline from outsiders—having routinely attracted international scrutiny during the country’s most high-profile moments dating back to the 1988 Seoul Summer Olympics.

Only about 5% of South Koreans said they had eaten dog meat in the past year, according to a recent survey conducted by an animal-rights group last month. More than 93% have no plans to consume it in the future, either. Those rejecting the practice cited their emotional response, animal cruelty and unsanitary conditions as their top justifications.

Other surveys, including one by a Seoul National University professor from 2022, also show overwhelmingly negative views of dog-meat consumption. Roughly two-thirds of respondents in that poll supported a dog-meat ban.

For centuries, dog meat was an exalted food in Korean cuisine, with locals believing that consuming it would improve one’s stamina and cool down the body—making it a prized dish during the summer months. In neighboring North Korea, an annual cooking competition for dog-meat soup is held every July. One local variety of the dish won a cultural-heritage award in 2022. Dog-meat consumption elsewhere across Asia, from Vietnam to Indonesia to parts of China, remains commonplace.

India, Philippines, Thailand and other places in Asia have banned dog meat, according to the Humane Society International, an advocacy group that opposes the practice. Some 30 million dogs are killed annually in the region for meat, the group says.

South Korea’s government estimates roughly 520,000 dogs were raised for human consumption in 2022, a 35% drop from five years earlier. Compared with the late 1990s, the number of dog-meat restaurants has fallen by about 75% to roughly 1,700 restaurants nationwide, according to government figures.

Outlawing dog meat has remained a delicate issue in South Korea—with officials, at times, denying the practice existed. Dogs aren’t classified as livestock, making oversight difficult. Large dog-meat marketplaces have been closed in recent years. But despite some official clampdowns, such as in Seoul, enforcement over the years has remained lax.

A national law, as passed on Tuesday, should remove any ambiguity. The cause took on new momentum in 2022, as President Yoon Suk Yeol took office. His wife, Kim Keon-hee, made a dog-meat ban a key pursuit as the country’s first lady. When Yoon’s term began, the couple owned four dogs and three cats. Kim has said she is unable to sleep for days after seeing images of dog-breeding farms.

Ahead of Tuesday’s vote, South Korea’s Dog Meat Association, which says it has roughly 1,000 members, has called any ban evil and hoisted signs that read, “The freedom to eat what you want trumps everything else!” The dog-meat law, the group argues, is driven by animal-rights groups looking to drum up attention for donations.

According to the association’s estimates, some 10 million South Koreans eat dog meat—roughly one-fifth of the country’s population—and the industry employs roughly one million people. The group wants compensation of roughly $1,500 per dog.

About one in four South Koreans own a pet, according to government data, with dogs the most common choice.

Write to Timothy W. Martin at Timothy.Martin@wsj.com