Did the companies continue to exist or did the shareholders lose their investment? Are there any other historical examples of what happens to a company's valuation when the country it is founded in collapses?
Can't speak to the USSR, but Nazi Germany did have a stock market, inherited from Weimar Germany, which in turn got it from Imperial Germany. Actually they had several, just as the US does today; there was (and is) a Frankfurt stock exchange going back to well before the unification, a Börse Berlin, the Hamburger Börse that dates back to 1558 (!), and a couple of others.
The Hitler regime did rather interfere with these exchanges - as early as 1933, soon after coming into power, they fired a lot of brokers for being Jewish, and in 1943 they imposed what amounted to price controls on the stocks. Free trading didn't resume until 1948, in a new building since the old one had been damaged in the Berlin fighting. (This refers to the Berlin exchange; I'm not sure about the others.) It seems likely that there was some kind of black market in shares, just as in everything else; you don't absolutely have to trade stocks in a formal exchange.
Firms that own bombed-out buildings and scattered workforces are not, of course, worth very much (hence the price controls); the index had stood at 27.75 at the time of the freeze, and was at 0.76 when trading resumed. Nonetheless, many prewar firms did survive the war in some form, as human capital if nothing else; what followed, of course, was the famous Wirtschaftswunder of the rebuilding. So, people who owned shares in, say, 1933, and who had managed to hold on to them through a somewhat disastrous fifteen years (that would mean physical share certificates printed on paper, which are more easily hidden than digital records but also more vulnerable to your house burning down) would be getting dividends again by the early fifties.
You asked about the USSR, which I don't know anything about, but I will note that Czarist Russia had a stock market, and the shareholders there were indeed completely wiped out in 1917. Not so much because the country collapsed - again, the firms might have survived that - but because the Communists, obviously, confiscated ("nationalised", if you like) the factories. The shares - I mean the paper certificates - would probably still be worth something as historical curios, though, if you found one in the attic.