I can think of about 100 different ideas honestly - but which one is going to work will of course depend on the child. Is it your child? Have you asked the child why they are talking? Is he/she motivated by anything that you could either offer up or take away in return for not talking?
FWIW, my dd used to do this - a lot - in kindergarten. We talked to her about it at a parent-teacher-student conference, didn't make a big deal about it, just explained that she was not supposed to be talking when the teacher was, that she was supposed to be paying attention, that when she talked, other kids were being distracted etc. - and that worked for my older dd. It most likely wouldn't work for my younger dd, who would instead figure out a way to go stealth and not be detected.
Best wishes,
polarbear