You're listening on your iPhone but using your computer. Device switching then says "okay since they're using their computer if any sound comes from the computer they should hear it" and then iMessage made a sound.
Had the thing you did been playing a YouTube video or Spotify then it would be weird if your AirPods didn't switch. I think it makes sense for notification sounds to not trigger switching by default but IDK what they currently do isn't totally unreasonable.