That's often because they were pursuing some philosophical riddle which they were more concerned about than the theories that would later become their legacy.
Cantor and Godel wanted to find God, and in their search, provided much of the early work for Turing and computer science to develop.
Einstein similarly wanted to know the mind of God, and in doing so contributed to quantum mechanics and invented relativity.
Bell's explanation of his theorem in a 1985 BBC Radio interview includes an appeal to the debate between free-will and determinism when interpreting his results.
TL;DR — Important scientific and mathematical breakthroughs often come from a deep exploration of metaphysics, epistemology, free-will, and ontological truth.