I think it's done because of a couple of reasons.
1. To be welcoming to the user
Error 404 never occurs in a situation where everything is alright. The same goes for connectivity errors, server down, etc.
You'd see marketing, forum-based, social and shopping websites doing this the most. Enterprise-level software or websites never do this. The reason being, these websites try to be appealing to the user at every step of the way. I can't tell you how much the Amazon error page made me want to keep coming back to it (see screenshot below)
Such designs help keep the users engaged even during a possible "diconnection"
2. To maintain continuity
An error page is a sign of loss of connection, misplaced url or an issue. By virtue of its existence, it means a lack of continuity in the web-surfing flow.
An attractive design not only keeps the theme and likeability of the page at-par with the rest of the website, it also makes it feel seamless. The Please Try Again, Go to Homepage, Go to Previous Page buttons seem less obtrusive and feel more like guides if a funny character or a cute doggo is sitting beside it
I also feel that the emergence of more creative Empty States and understanding that they are important have added to this trend