Abu Masud Uqba bin Amr reported,

I was beating a slave boy of mine when I heard a voice behind me saying, "Know, Abu Masud, that Allah has more power over you than you have over him."I turned around, and there was the Messenger of Allah ﷺ.I said, "Messenger of Allah ﷺ, he is free for the sake of Allah."He said, "Had you not done so, the Fire would have scorched you," or, "the Fire would have touched you."

Azami 14007

Sahih

عن أبي مسعود الأنصاري، قال

كنت أضرب غلاما لي، فسمعت من خلفي صوتا: اعلم، أبا مسعود، الله أقدر عليك منك عليه، فالتفت فإذا هو رسول الله ﷺ، فقلت يا رسول الله ﷺ، هو حر لوجه الله، فقال: أما لو لم تفعل للفحتك النار، أو لمستك النار.

الأعظمي 14007صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Superior moral conduct
Chapter
Noble character
Section
Expiation for one who strikes his slave is to free him
Numbering
Jami al-Kamil 14007 (2nd edition)
Azami 14007