When talking about the world wars, it's fairly obvious that WWII occurred as a result of the consequences of the WWI peace treaty. So the question about what caused both world wars is really a question of what caused WWI. I happen to have been going back through this history recently, so it's well been on my mind.

There are many putative causes for WWI. Wilson, for example, favored the idea that secret treaties was what caused the mess to blow up, but the evidence is fairly against it (basically, everyone who was making the decisions either knew or correctly guessed what the countries were going to do, and the only real question in the July Crisis was what Britain would do, and the British themselves didn't seem to have made up their mind). Other theories include the idea of "timetable war": the timetables for mobilization were too inflexible such that starting them meant they had to be seen through to completion, but that's more of an excuse that the military gave to justify their actions. Franco-Prussian war comes up a lot, but as I'll shortly explain, France wasn't particularly in an offensive war mood.

To understand what caused WWI, it's helpful to see the state of the countries in the immediate years preceding the war. Britain was distracted with the Irish Home Rule crisis (so much to the point that the July Crisis didn't really get noted until after the Serbian ultimatum!), and many feared that revolution or civil war was imminent. France, for its part, had had its internal divisions sharply exposed by the Dreyfus affair, and the Third Republic was a succession of weak governments. With the memory of the Franco-Prussian war, plus a strong socialist/reactionary divide, and the embarrassing and very public Dreyfus affair, its army was heavily demoralized and was in no mood to fight an offensive war, particularly not against Germany with no allies. Russia had lost the Russo-Japanese War in a shocking defeat, and was using primarily French funds to rebuild and modernize their army (which was dooming the Schlieffen Plan, as Germany planned to knock out France while Russia was mobilizing its army). The Russian government very nearly fell after their lost loss in a war, and they didn't expect to survive the next war if they lost. Austria-Hungary was basically two countries (Austria and Hungary) who had to work together but couldn't due to deep internal divisions. Some elements of the government (including Franz Ferdinand) saw the Balkans as a way to solve the problem by creating a third internal division that could overrule Hungarian objections to everything. Italy was a weak power who everyone pretended was a great power. The Ottoman Empire was slowly crumbling and featured more or less the last virgin land for colonial expansion.

Last of all is Germany. By 1910, Bismarck's foreign policy had been completely torn up, and Germany saw itself surrounded by the Triple Entente of Britain, France, and Russia. Its only allies were Austria-Hungary, which looked dangerously unstable if it lost a war, and Italy, which had more or less adopted a policy of be-friendly-to-everyone and couldn't be counted on to join in a war (in 1914, it did declare its neutrality). Internally, Germany had a rather sharp revolutionary/reactionary divide, a rather common feature in Europe. German foreign policy in the early 1900s was especially bad, as Germany started antagonizing most players, not helped by the tendency of Kaiser Wilhelm II to say things without consulting advisers or considering any coherent policy (which sounds unfortunately familiar to present times). Britain's traditional policy with respect to Europe had been to maintain a splendid neutrality and only intervene to maintain a balance of power on the continent; Germany hoped to convince Britain that France and Russia were greater threats and to bind itself to an alliance with Germany--which was shockingly naive, especially after Germany decided to try for naval parity with Britain in the naval race. The most Britain would have been willing to commit to would have been to stay out of certain offensive wars and to help mediate disputes and restrain other countries, which was far less than Germany wanted, given its fears of Franco-Russian two-front war.

In the lead-up to WWI, there are a series of instructive crises. The first is the Moroccan Crisis, when Kaiser Wilhelm objected to French influence in Morocco, with the hope of being able to break the Entente Cordial (prelude to the Triple Entente) between France and Britain. It didn't, and Germany found no one willing to support its side, so it was forced to back down with no real gains. A few years later, Austria-Hungary annexed Bosnia, to which Russia stridently objected; this time, France and Britain convinced Russia to back down with no real gains from the issue. Then France moved to formalize its status in Morocco as a protectorate, to which Germany (not without reason) objected. Germany bungled the issue by asking for unreasonable demands in compensation (great way to lose support!), and was ultimately forced to settle with minimal compensation. Then Italy invaded the Ottoman Empire to get itself a colony in Libya, which convinced the Balkan minor states that it was finally open time to carve the Ottoman Empire up. The great powers were so terrified of the resulting destabilizing effects that they forced the war to a conclusion, but the Balkan states themselves, upset with who had gotten the spoils, immediately fought each other within a few months to change hands.

In these various crises, major powers had mobilized and prepared for war, sometimes as a bluff, sometimes a fear of war being declared on them (better to mobilize before war gets declared on you), and sometimes as a genuine attempt to get to war first. So when a Serbian terrorist assassinated the heir to the throne of Austria-Hungary, many observers more or less expected that yet another muddling-through would happen. So why didn't it?

It's worth noting that all the major countries had major internal divisions, and governments generally saw war as a way to bring their country in unity--and, ironically, also saw the divisions in other countries as indicating that they wouldn't come together in war (the first part ended up being the case--only the Ottoman Empire failed to see unity in the war, and even then, the Arabs mostly did decide to wait until after the war to move forward on their grievances). There was also a general sense, particularly among the military, that war was inevitable and that it should be timed opportunely. The change in the relative qualities of the armies mattered too: the German's war plan (emphasis on the singular) had them defeating France while Russia mobilized, so the fact that the Russian army was improving, and the military logistics in European Russia in particular were improving, meant that their time frame for defeating France was shrinking.

Ultimately, it was the July Crisis that started the war. Austria-Hungary, after dithering a bit, made up its mind to go to war against Serbia and crush it. The ultimatum it gave was made with the express purpose that it would be so insulting that it wouldn't be accepted. When Serbia did fail to fully accept the ultimatum, Austria-Hungary suddenly got cold feet, but the German high command pushed Austria-Hungary to actually declare war and defeat Serbia before anyone had time to respond. Even then, the initial reaction of countries like Britain and Russia was to try to push Austria-Hungary to achieve just a limited war ("halt in Belgrade"), which Austria-Hungary rejected. Russia's reaction was initially to mobilize against just Austria-Hungary, before the military convinced the tsar to include Germany as well. Germany seems to have made up its mind to go to war against Russia before the declaration of Russian general mobilization, although it delayed its announcement until after the war. Germany's war plan meant invading France and Belgium, and the Belgians and French made their preparations for mobilization for defense at the same time, although France was very careful to maintain that the war would be purely defensive for the purposes of British opinion. Italy decided it couldn't handle another war in the aftermath of its Libyan campaign, and declared its neutrality fairly early on. Britain throughout the entire crisis dithered and essentially deadlocked on whether or not to go to war. The German invasion of Belgium provided enough incentive to get Britain to join the war against Germany (the Germans were distraught, having thought the entire time that Britain would remain neutral).

So what caused the war? In the end, it's hard to boil it down to a single statement that does justice to the situation. My best attempt is that WWI was caused ultimately because the people who had been keeping the peace suddenly decided to not do so this time. There are many reasons why the countries thought that war was justified, indeed potentially useful, and there are many reasons why the countries ended up dividing Europe into two main opposing alliances. Such a situation, however, need not lead to war: the Cold War is a great example of how a dueling alliances need not break out into general war (and the reasons why are similarly debated).