"An unknown error occurred" when signing into iMessage account with Apple ID on macOS

I used to have the same error on my device once.

In my case it helped resetting the NVRAM, which is one of the recommendations in Apple's documentation.

Reset NVRAM or PRAM on your Mac

⌥ + ⌘ + P + R

Shut down your Mac, then turn it on and immediately press and hold these four keys together: Option, Command, P, and R. You can release the keys after about 20 seconds, during which your Mac might appear to restart.

https://support.apple.com/en-us/HT204063