Nice connection!
The simple answer is that chords come from scales, and after transposing, all these chords are in the same scale. The last chord of song 2 is the same as the first chord of song 1.
In the first song B-C#-D#m are the IV-V-vi of the F# Major key (scale).
The second song's chords are Dm-Em-F, which would be ii-iii-IV of C major, (or transposing, G#m-A#m-B in the key of F#.)
If you put them in the same key it would be G#m-A#m-B-C#-D#m. I think you'll hear the connection if you play them together in that sequence. This is the idea of "diatonic chords".
Also, the chords of G#m and B share two notes (G#-B-D# & B-D#-F#), as do each of the other chords when paired (A#m and C#, B and D#m), because chords are built in thirds. (This is what PiedPiper was pointing out in their diagram.)
The terms "relative minor" and "relative major" are helpful.
G#m is the relative minor of B Maj.
The melody is also coming from the same key (same 7 notes in the scale) and is usually going to match one of the chord tones. So it's going to work with either G#m or B because the chords share two notes.
Finally, this song should put all the ideas together for you: My Favorite Things. (check out the original version from Sound of Music)
The first part of the melody repeats itself over two different chords related in the same way as in your example. First over D#m then over B. Then it even substitutes D#min with D#Major and the other minor chords with their relative Major chords appropriately. Listen to how that song's harmony shifts underneath the repeated melody and how that gives it a different flavor each time. Enjoy!