How does Signal Messenger's Safety Number Works? Why do they recommend the user to verify the 60 digit code in person or via other medium of communication.

How is the security code generated and work on Signal and Whatsapp Messanger?
Here is a detailed blog post about the safety numbers:
https://signal.org/blog/safety-number-updates/

They are unique per conversation and basically consist of hashes of your and your contact's public long-term key.

You should compare them if you want to be sure that there is no man-in-the-middle.