Rabbi Zilberstein has a "teshuva" in which he was asked about the following (shortened for brevity):
Someone was sitting on a bus behind two girls who were speaking lashon hara about a third girl who had just gotten engaged. The lady sitting behind them leaned over and told them that it was her son who was engaged to this girl and she was so glad to have overheard all of their earnest remarks about the girl, as now the mother knew to save her son from this relationship and she was going to break off the shidduch. The girls attempted to convince the woman that they had just been joking and exaggerating, and that really she was a great girl, but the mother refused to be convinced. The girls began to cry, but the woman remained unmoved. Finally, before getting off the bus, the woman told them that she wasn't actually the boy's mother, but she could have been.
R' Zilberstein was asked if the suffering she had caused these girls in order to teach them not to speak lashon hara was permitted, and he replied that it was not.
So from here it seems that at least R' Zilberstein holds Onaas Devarim is not permitted in order to teach a lesson.