A massive meteor bombardment destroyed the Earth's ecosystem. So humans and a massively intelligent AI did what it could to save as many people as possible.
The AI needs to keep some outside the Matrix as a control and insurance against problems inside the Matrix. The AI spreads the idea that the Matrix "victims" are slaves and provide energy to the AI to keep the outsiders outside (even though the energy source claims are obviously ridiculous - the people in Zion are profoundly ignorant and bordering on outright stupid).
The AI kills the thousands of people in Zion every hundred years or so when they get aggressive enough to start destabilizing the Matrix, thereby threatening billions.
This makes more sense than the silliness of the movies anyway (which admittedly isn't saying much).