Steven Pinker and many other evolutionary psychologists believe that music is cognitive cheesecake. That is, we have a lot of cognitive faculties working in concert, and musical appreciation and ability emerge out of the synthesis. But there wasn’t direct selection for music, as such. Musical appreciation then may not be adaptive.
And yet like reading and writing music clearly co-opts part of the human brain in terms of functional localization. There are people with brain injuries who can not speak well who nevertheless can sing well, and can communicate through song.
But perhaps most important, just because a trait did not emerge due to natural selection, does not entail that it might not be subject to later selection. One can make arguments that musical ability was adaptive at some point in human existence on the individual scale. But I have something else in mind: music is functionally important in war. Military marching bands did not arise coincidentally, music as an accompaniment to the march and a way to communicate and rouse the troops to action have been part and parcel of winning and executing battle. Music triggered social change in the 1960s.
I think much the same is probably true of religion. My own position is that the shamanic/primal form of religious belief bubbles up out of our cognitive architecture as a side effect of other processes. But this byproduct can be co-opted by cultural evolutionary selection, and reshaped into something with functional utility.