In order to fully understand Austro-Hungarian declaration of war on Serbia, an event that sparked the World War 1, we need to go back a while and take a look at the situation in Europe and Balkans especially at the beginning of the 20th century.
In 1903, a secret military organization called the Black Hand performed a coup in Serbia, killing King Alexander Obrenovic and his wife, Queen Draga and brought Petar Karadjordjevic to the Serbian throne. The move was met with severe judgment from several European powers, especially Austria-Hungary, who hold a great influence on the late King Alexander. The new King Petar was a Russophile and immediately started working on establishing closer ties with Saint Petersburg and Tsar Nikolai.
At the time, Austria-Hungary was a conglomerate of nationalities. The three largest were German, Hungarian and Czech. The fourth largest group were Serbs. More than 5.6 million Serbs lived within the borders of the empire, making about 10% of the population. This made Vienna court extremely sensitive to any clamors of liberation and attempts on part of Serbia to claim those parts of empire inhabited with Serbs.
The first attempt to bring Serbia to heel was made in 1906, when Austria-Hungary imposed crippling tariffs on Serbian imported goods, especially pork, which was the main export for Serbia, starting the Customs War or Pig War, as it was called in Serbia. By 1908, Serbia managed to find other markets for its pork and other commodities, effectively defeating the purpose of sanctions imposed by Vienna. As a result of these events, irredentist movements in Serbian parts of Austria-Hungary grew and started to create serious trouble for the empire. Vienna tried to crush the movement with repression, but that only made it worse.
In the meantime, Germany, Austria-Hungary’s closest ally, was looking for an excuse to declare war on France. Germany didn’t have a chance to participate in the grab for colonies like Britain and France and they felt the only way to get their hands of some of the profitable overseas possessions was through war. They had the most powerful army in Europe and their navy was increasing every year, although it was still lagging far behind the Royal Navy. Paris and London were aware of German intentions and formed an alliance, which was later joined by Russia. The alliance was called Triple Entente. Entire Europe was busy increasing its armed forces to unprecedented levels.
The Arms Race, especially the Anglo-German naval race, was creating a lot of tensions. All it was needed was a spark and the whole keg of gunpowder would explode. The first such spark came in 1908 when Austria-Hungary annexed Bosnia. Fortunately, cooler heads prevailed and the war was averted. Four years later, the Second Moroccan Crisis erupted, when Germany tried to exert its influence in that country. The tensions were so high that Britain has ordered the Royal Navy to prepare for war. Once again, a flurry of diplomatic activity managed to stem the tide and Europe breath a sigh of relief.
Then in Jun of 1914, the unimaginable happened. Crown Prince of Austria-Hungary, Archduke France Ferdinand, was in an official visit to Bosnia. While driving through Sarajevo, he was met with a group of Serbian nationalists from a group called Young Bosnia and one of them, Gavrilo Princip, managed to assassinate both him and his wife. The group had close ties with the Black Hand and Serbian secret police, although it is unclear whether official Belgrade was aware of these connections.
The assassination caused widespread outrage. German Emperor demanded that Serbia be punished and in a meeting with Austro-Hungarian Foreign Ministry Count Alexander von Hoyos in Berlin a week after the assassination, issued the infamous Blank Cheque to Vienna, promising its support regardless of consequences.
Encouraged by this, Austria-Hungary issued an ultimatum to Serbia, requiring, among other things, the admission of their investigators into the country to conduct an investigation. Serbia agreed to all but two of the demands, out of 10 listed in the ultimatum. Austria-Hungary was not satisfied with this and on July 28, 1914, officially declared war on Serbia. In response, Russia orders mobilization and demands Germany to stand aside. Germany refuses and instead, declares war on France, Russia, and Britain on August 3, immediately attacking neutral Belgium. Austria-Hungary also declares war on Russia and World War 1 begins in earnest.