I know that after Pearl Harbor the Japanese diplomat Kurusu was interned until an exchange of diplomats could be arranged and then was able to return to Japan. I also recall that in the more distant past, sometimes envoys/messengers from foreign countries were murdered such as how Vlad the Impaler treated Turkish envoys.
So limiting it to the last 500 years I would be interested in cases were diplomats were either imprisoned or executed or at least in danger of being treated as criminals rather than being allowed to return to their home countries after war had broken out.
The question as it currently is is a bit vague. Does executed or imprisoned by non-state actors count? That one is fairly common, some well-known examples are the 2012 Benghazi consulate attack or the kidnapping of four Soviet diplomats in Lebanon in 1985. Wikipedia actually has a list of ambassadors who were killed in office. Most of them were killed by criminals or terrorists, some were killed by factions in a civil war and a few by foreign governments (neither their own nor that of their host country). The Iranian Hostage crisis (which has already been mentioned in the comments) may be a bit of a borderline case, as may be the killing of Iranian diplomats in Afghanistan in 1998 (the latter in so far as the Taliban claimed it was not their intention to have the Iranians killed)
A rather famous example of deliberate violation of diplomatic immunity is the siege of the foreign embassies in Peking in 1900, and the murders of Clemens von Ketteler and Sugiyama Akira in the days before.
Another well-known example is the disappearance of Raoul Wallenberg in Budapest in 1945.
Taken from this web-page: The arrest of Constantin Diamandy, Romania's plenipotentiary minister in Petrograd, on Lenin's order.
On the evening of December 31, 1917 (January 13, 1918, new style), the plenipotentiary minister of Romania in Petrograd, Constantin Diamandy, and the staff of the Royal Legation of Romania were arrested by the Bolsheviks, under Lenin's order, and taken to the St. Peter and Paul prison. This unusual incident in the diplomatic world was the reply of the Soviet government to the tensions between the Romanian army and the Russian soldiers in Moldavia. The soldiers of the 194th regiment of the Russian 49th Division, who were stationed between the cities of Bacău and Roman, near the front line, came into conflict with the Romanians, and were ordered on December 27, 1917 to lay down their weapons. The Russians refused and threatened to use force to reach Roman, in order to release a Bolshevik leader held by the Romanians. The Romanian army was forced to open fire. Several Russian soldiers were injured, the rest were disarmed by the Romanians or fled. The disarmament of Soviet troops attracted a harsh and unusual response from the Soviet government. The arrest of the Romanian Plenipotentiary Minister in Petrograd provoked protests from the entire foreign diplomatic corps in Russia.
Following the protest of the diplomatic corps accredited to Petrograd, after two days of detention under humiliating conditions, Constantin Diamandy received a note from the Soviet government conditioning his release and that of the personnel of the Royal Romanian Legation with the release of the Russian troops detained on the front. However, Romania's plenipotentiary minister in Petrograd refused any negotiations: “I replied that I would not accept that my release be subjected to any condition and that, in the context of such a horrendous violation of International Law, I would absolutely refuse to negotiate state issues while incarcerated”.
Constantin Diamandy agreed to leave the prison only after writing a report stating clearly that he refused any negotiations with the representatives of the Russian government. After his release, Diamandy was expelled from Russia, and the Soviet government decided to break diplomatic relations with Romania (January 13/26, 1918) and to seize the Romanian Treasure, located in Moscow.
The Soviet of the People's Commissars of the Russian Soviet Federative Socialist Republic claimed that Constantin Diamandy's arrest “for a short time” took place “in protest” against “the crimes of the Romanian military and civil authorities”.
Not exactly a world war causing event, but fairly recent: in 2014 a Russian diplomat, Dmitri Borodin, was arrested for drunk and disorderly conduct, as well as insulting the police in his house in Scheveningen (part of The Hague) in The Netherlands.
The Russian government asked questions, and ordered his release. The Dutch government apologized, and released him. 2 months later he was recalled back to Mother Russia, who wasn't very pleased with the behaviour of her drunk son.
Dmitri was the second highest ranking diplomat on the embassy. When this happened, it was pretty big news in The Netherlands.
https://www.nu.nl/binnenland/3720884/russische-diplomaat-schold-agenten.html (In Dutch only)
Is a murder of a diplomat considered a "violation of diplomatic immunity"? If yes, here are two recent examples: