Marx did not dismiss religion as merely some stupid, inane preoccupation of the masses. After all, Marx was the son of a rabbinical family. In his Contribution to the Critique of Hegel's Philosophy of Right he wrote:
Religious distress is at the same time the expression of real distress and the protest against real distress. Religion is the sigh of the oppressed creature, the heart of a heartless world, just as it is the spirit of a spiritless situation. It is the opium of the people.
The abolition of religion as the illusory happiness of the people is required for their real happiness. The demand to give up the illusions about its condition is the demand to give up a condition that needs illusions. The criticism of religion is therefore in embryo the criticism of the value of woe, the halo of which is religion. Criticism has plucked the imaginary flowers from the chain, not so that man will wear the chain without any fantasy or consolation but so that he will shake off the chain and cull the living flower.
Karl Marx was not philosophically opposed to religion. In fact, he thought that religious belief is important to an oppressed people who need illusions. At the same time, Marx did not believe that God creates man. Rather man creates religion and a mythical God.