I am not a very religious person but I’ve done a fair bit of reading, and it seems clear to me that in this passage Sagan is drawing from a very old and very deep well. This was something embedded in his cultural background and he might not have been aware of it himself.
Specifically, the sequence that goes from a profound aesthetic experience, to awe at the scale of the Universe, to the futility of human selfishness, and finally to the conclusion that we ought to treat each other kindly, is very much not obvious. It just seems obvious to us (and to him) because that’s the culture where we have been raised.
Now, I’m not saying that the experience described in this passage is exactly the same as the Buddha achieving enlightenment or a Christian saint receiving the beatific vision. But they are not completely different either.