Here is my take on the story - using bits from the Anime shorts done on the matrix as well.
1) In the machine war the machines set themselves up as a single country and faction unto their own right; however they are still producing for humanity because that's why they were made in the first place - it is their function.
2) The war begins. Now for this we have to assume that the machines have reached a point where they understand the concept of existence and preservation of themselves (at the very least as a collective unit). It might also be part of their original programming to preserve themselves - therefore the war begins and the machines do fight back.
3) As the war advances humanity blots out the machines primary energy source; I expect this forces the machines to shift their tactics. They have to end the war quicker and by this point it seems that peace is not an option on the table. Therefore machines, being machines, shut down all unnecessary elements of their civilization. If you are not part of the war effort you have no use and are deleted with the view that lost machines will be replaced after the war.
4) Now alongside that the machines also have another problem. They exist as an entity to serve mankind, without that core need they have no function. And with no function and purpose the machines would be forced to delete their whole society as per their core programming (at this stage there is no matrix to hide in and likely machines have significantly reduced independence than we see in the film).
5) Now you've got machines who must win, but cannot wipe out their enemy, however you've also another element. When the power was reduced the machines shut down/deleted any that were not needed and focused purely upon the military effort. The machine put in charge (or the one with the most significant control) also came to realise that without the war; when its all over, will also be deleted. This machine does not want this, it has a sense of self preservation at the personal, not just collective level.
6) Thus when the machines win they have a dilemma to which the matrix presents an ideal solution. If humans are grown within the machines they can be served at a mental and physical level near perfectly. The machines can continue to serve mankind without any need for war. The matrix sounds like an ideal option!
However during this stage there are still humans free and fighting back in small cells; the military machine still has a reason to be in charge. So it oversees the construction of the first matrix.
7) This explains why the early matrix systems fail. I suspect it was not due to it being so perfect that human minds rebelled, but that the military machine found a way to sabotage the system. It might even have been as a result of it adjusting the numbers so that those minds which reject the matrix or question it have that effect amplified and thus destabilizes the whole system. It enters the early cycles of extreme build-up and extreme collapse. A system where you need the military in charge because of this continued conflict.
8) Now enters the Oracle program who finds a way to build a working matrix without the extreme gain/loss effect. This program finds a way which people can "choose" what "side" to be on as part of the matrix. This is a much better solution; its less extreme and yet because humans removed from the matrix are not "at war" and just "rejecting" it they are not the enemy at that stage. They are thus released into the world to survive on their own. Their interests are served in that manner.
However they are encouraged into rebellion and conflict. They are free and the don't "have" to war, indeed in the early stages of a new Zion they likely live in a very peaceful setup; if under watch by the machines. Then the continued pressure and the hints of more inside the matrix who want out rises. The machines allow the humans to release each other, even though it causes conflict.
9) Neo - now Neo is important because he is again the lack of balance; likely tipped by that military machine. Neo is what ensures that things retain a warlike edge, what keeps humans as a threat.
By this point there are cracks, indeed many intelligent machines who are more wordly in their views do likely see the pattern, but they play along because it preserves them as well. Others such as the Architect and Mr. Smith are, however, totally a part of this system to the point that they cannot and do not question it. At least Smith was.
Smith when he is released (and its suggested its not totally Neo's doing - I wonder if those cookies contained code!) he gains a more worldly view. He sees the cracks in the system; that the war is fake and prolonged. He, however, is a newer generation of machine and thus whilst his purpose drives him he is also capable of existing beyond it. He can make his own.
That's the threat Smith presents; he can and will destroy humanity, but will also destroy the machine people as they currently are in what one could call a decadent state. It's a threat to all machines as his cloning of himself (which considering the machines do no use cloning suggests that its part of their core programming to not perform). Indeed I wonder if Smith's prime difference is that he has no original code. That he, once free, is not a part of the system in any way shape or form. It might even be that he was created as such; a totally 100% new machine, no previous code nor rules to follow - kept in check whilst he was part of the system (mostly - recall he removed his ear-peace) until he was released from it.
This argument also means that Saffie was a much more key character than the last film made her out to be. She is not the first, but she is one of the many new machine generations which is operating without singular purpose and likely without much or any of the original code requirements the machines were made under. As such she represents an evolution in the machines of a significant magnitude because if she survives and is accepted there is the potential that the war can actually end.
The ending of the 3rd film suggests that only a truce has been met; that the old order is still in control and still subject to the same limitations it was under. However that Smith was released from those binds and that new machines are being made also released from them; this I think is what the oracle is working toward, but that it will take time.