For a brief moment on Tuesday, it looked like the United States and Iran might go to war. Several days ago, a U.S. airstrike killed Iranian General Qassem Soleimani. And then on Tuesday, Iran retaliated with rocket attacks on Iraqi bases housing American troops.

Markets reacted immediately: The stock market dropped, oil and gold prices spiked, and yields on U.S. Treasuries fell.

By Wednesday morning, things were smoothing out, as statements from both sides suggested no one wanted to escalate things further.

But why exactly do the financial markets react the way they do to these sorts of crises? We all have a vague understanding that "market turmoil" is a natural response to things like terrorist attacks and international confrontations. But what, at a basic mechanical level, is actually going on?

The central dynamic here is what's called the "flight to quality," or the "flight to safety." Basically, investors are selling off riskier assets — which usually means stocks — and plowing the money into safer assets — which usually means U.S. Treasuries.

Crudely speaking, demand for stocks is a function of how profitable investors think a company will be. If stock buyers think business might go bad, that's a reason to sell: Not only will your own dividend payments be lower in the future, but what other people might pay you to buy the stock falls too — possibly below what you paid for it, and then you've lost money.

When something like this week's tit-for-tat between Iran and the U.S. happens, participants in financial markets worry they could spiral into some broader crisis that seizes up economic activity and hurts business and profits across the board. That's why, for instance, the S&P 500 dropped 1.7 percent on news of the strikes, before recovering. It wasn't a momentary loss of confidence in any particular company, but in business profits as a whole.

How rational those fears are is another matter. The stock market has actually proven pretty resilient in the face of previous shocks, like 9/11 and the 2008 financial crisis. Nonetheless, such fears tend to be a perennial feature among financial market participants. The key point is that stock values are tied to unpredictable ups and downs out in the economy and the real world, and that makes them an inherently risky asset.

But if investors are trying to protect themselves from risk, why ditch stocks for U.S. Treasuries?

Essentially, investors are looking for assets they are the least likely to lose money on — assets whose value is as disconnected from random events in the world as possible. And the gold standard for that sort of investment is U.S. Treasury bonds.

A bond is a way for a company or government to borrow money. The buyer pays the seller a certain amount of money for the bond, and the seller agrees to eventually pay that principle back, plus interest payments in between. The person who bought the bond could always sell it onward to someone else — for more or for less than what they initially paid for it, depending on market demand. But if you buy a bond and just hold it, whatever entity that sold you the bond is obligated to pay you back the principal in full.

Now, if you buy a bond from a company, it's always possible the business could fail and go into bankruptcy. In that case, you might lose some of your money. Same thing for a state government bond, though in practice that's exceedingly rare. But the U.S. government is literally the creator of U.S. dollars. It can never "run out" of money, or "fail" in the conventional sense that a private sector company can fail. It could choose to default on its bonds, but that would be a political decision, not an involuntary act forced by economic reality. It can always create the money necessary to pay off bond obligations denominated in its own currency — an important distinction, since countries (usually poorer or developing ones) do sometimes sell bonds denominated in currencies they don't control. The same is true for other advanced nations that issue their own currency and don't try to peg the value of that currency to any outside metric, like gold or some other nation's currency.

Of course, it's still technically possible those governments could set off some kind of hyperinflation crisis through economic mismanagement and wreck the value of their bonds that way. But in practice, if you're an advanced and diverse economy with lots of opportunities for investment, hyperinflations severe enough to wreck the value of your bonds are really difficult to create. Moreover, the U.S. enjoys an added layer of protection in this regard, thanks to the U.S. dollar's status as the global reserve currency.

In short, if you buy a U.S. Treasury bond, or a bond from the Canadian or German government or some such, then — barring the complete collapse of western civilization — you're going to get your money back.

That's why, when the Iranian rockets started flying on Tuesday, yields on U.S. Treasuries briefly fell from around 1.8 percent to 1.7 percent. "Yield" is a slightly technical term, but a falling yield for a bond basically means demand for the bond is rising. That's also why, when international crises like this hit, you often see yields fall on bonds for other governments that oversee advanced economies.

The momentary spike in gold prices is a function of the same dynamic. For people who do think a hyperinflationary collapse of the U.S. dollar is a real possibility, they often figure gold is a safe alternative: the currency of choice that might arise in the post-apocalyptic world. Once again, you could debate the rationality of this logic (to put it mildly). But that's why, when crises like this hit, you often see demand for gold — and thus the price of gold — jump as well.

Finally, there was the jump in oil prices in reaction to Iran's attack. This is even more straightforward: The Middle East is a big source of the world's oil, and the worry among markets is that a wider conflict there could cut off oil supplies relative to global demand. That would mean a spike in oil prices, and oil futures momentarily traded in anticipation of that increase. But again, once it became clear the confrontation probably won't escalate, oil futures fell back down.

Needless to say, there are many other factors that can complicate these scenarios. But if you want to understand market reactions to crises like America and Iran's mutual strikes, the "flight to safety" is the key dynamic to keep in mind.

Want more essential commentary and analysis like this delivered straight to your inbox? Sign up for The Week's "Today's best articles" newsletter here.