I've been recommended unrelated songs a few times on Spotify. Many times the recommended songs do not carry the mood of the previous song. Have you also experienced the same as songs of different moods are recommended? (And by the 'mood' I do not mean 'genre')
well, the answer is not simple and it lays on many layers but let me try to simplify.
Spotify song features
The mood factor as simple as you mentioned in a song. if we go as deep as signal processing for audio analysis topic, there are actually low-level features such as 'zero-crossing rate', 'spectral centroid', 'mel frequency cepstral coefficient' and such. Yet many of these low-level features are not extracted from the audio signal directly, they are commonly used with disciplines/formulas such as 'short time fourier transform', 'analysis windows (frames)' etc.
Using these low-level features in various calculations and formulas, spotify acquires high-level features such as "danceability", "energy", "loudness", "speechiness", "acousticness", "instrumentalness", "valence" etc. these high-level features are commonly used on many music platforms that has any recommendation algorithm in it. So lets say not only "mood" affects the recommended songs, there are many parameters that may match your previous listening footprint but differ on others. It's not that easy to guess.
If you want to research more here's a link: Spotify API reference - Get Recommendations Based on Seeds
Also, without talking sharp, recommendations are usually not as pure as I mentioned above. Yes, they are based on your listening history, but also trending popularity and commercial value in your country, city, age, gender etc.