JOHANNESBURG — Two white South African farmers were sentenced to a combined 41 years in prison on Wednesday for killing a black teenager whom they had accused of stealing sunflowers, bringing to a close a case that had inflamed tensions in a rural town and exposed the nation’s enduring racial divisions.

During a hearing at the High Court in Mahikeng that was packed with black and white South Africans, Judge Ronald Hendricks described the actions of the farmers, who were convicted of throwing the 15-year-old boy off a moving truck in April 2017, as “disgraceful” and “appalling.”

The boy, Matlhomola Mosweu, died after breaking his neck. His death set off riots in the town of Coligny, about 125 miles west of Johannesburg.

The two farmers — Pieter Doorewaard, 28, and Phillip Schutte, 35 — were found guilty of murder, kidnapping and other charges. Mr. Doorewaard was sentenced to 18 years in prison, and Mr. Schutte to 23 years.