ego + ism is just about the complete story, but not quite.
ego + ism = egoism. In fact, when French coined the word in 1755 (Online Etymology Dictionary ), they coined it as égoisme; and when Greek took the word in from French, they kept it as εγωισμός.
But someone somewhere early on found that ego-ism sounded weird. With good reason: Latin nouns ending in –o would not be stuck next to an –ism normally—a bunch of inflectional and derivational affixes would intervene (natio > natio-n-al-ism). Ego is a pronoun, not a noun, so those kinds of affixes were just not available.
So, because ego-ism looks strange, someone decided to stick in a consonant to break up the ego– and the –ism. The consonant here happened to be a –t-; WordReference.com Dictionary of English (which I believe is OED material) thinks it was by analogy with despot-ism.
There is a recherché distinction that some people have made between egotism and egoism in English: egotism is a bad thing, egoism isn’t. But that distinction is pretty much made up, and noone really bothers with it any more.