Most of the great mathematicians were religious. As to why is that, we can only speculate. I personally think that while exploring the beautiful worlds of numbers they discovered order and perfection and that got them thinking about Divinity.
I think it's because they hadn't heard of evolution or big bang theory yet. There are probably way less religious scientist today than there were in the previous centuries.