They came about because Gaza was an open prison, denied any opportunity to build any domestic industries, or of Palatinians allowed to travel, and because the Fatah party were seen as corrupt, not doing anything, and almost complicit with Israel in their racist policies.They came about because, the Muslim Brotherhood, which they were part of, were designated a terrorist organisation by Egypt and Saudi Arabia, amongst others. They were not able to function and splinter groups popped up, Hamas being one.
Political parties do not form in a vacuum. They certainly don't get elected with an overwhelming majority in a vacuum.