Its sometimes an unpredictable relationship because it depends on the movement of money, not its current value.
When the US stock market is bullish, foreign investors want to buy USD so they can use dollars to buy US stocks, and they sell e.g. EUR and JPY to get them. This drives the USD up. The investors within the country already have the currency so they don’t need to go through any exchange process.
However, the US has the largest national economy by all measures and as a result the USD is a “reserve” currency, i.e. one that all investors and banks will wish to hold as a stable reserve they can easily allocate wherever needed. Partly why commodities generally are traded in USD, even if not produced in the US.
This matters as the US stock market is also a barometer for risk tolerance in the global markets generally, so when money is pulled out of the US stock markets, it tends to get pulled out also from other equally or more risky assets. If you sell US stocks you’ll be paid in USD, which is good to have because you can invest it in dollar-traded commodities like oil or gold etc. USD are also easily exchangeable if you want to use them to buy e.g. CHF or JPY. If the US stock market falls far enough, USD will be the last readily tradeable currency capable of being used to buy anything, so a falling US stock market and a rising USD indicates the big players expect really stormy times ahead.