MANILA — A Philippine court convicted three police officers on Thursday of the murder of a 17-year-old boy and sentenced them each to up to 40 years in prison, in the first such convictions in a wave of killings prompted by President Rodrigo Duterte’s war on drugs.

Nearly 5,000 people are thought to have been killed by the police, and many more by unofficial militias, since Mr. Duterte swore during his 2016 presidential campaign that he would hunt down drug sellers and users and “dump all of you into Manila Bay, and fatten all the fish there.”

But until now, the rampant killing has taken place in an atmosphere of impunity, with Mr. Duterte insisting that he would pardon any officers found guilty of murder while carrying out his crackdown. For the first time, his vow will be tested.

“The use of unnecessary force or wanton violence is not justified when the fulfillment of their duty as law enforcers can be effected otherwise,” the judge in the case, Rodolfo Azucena Jr., said in his ruling on Thursday. He said a “shoot first and think later attitude” could never be justified.