In a sense, it’s partially due to “bad luck”. Elements have to share stability with their neighbors. In particular, taking two adjacent elements, and considering two different isotopes with the same number of nucleons (called isobars), at most one of them can be stable.

Consider ^{96}Mo vs. ^{96}Tc. ^{96}Tc has 43 protons and 53 neutrons, while ^{96}Mo has 42 protons and 54 neutrons. As it happens, ^{96}Tc undergoes electron capture, converting one of the protons into a neutron, which turns it into ^{96}Mo. On the other hand, we have ^{98}Tc, which undergoes beta decay, converting a neutron into a proton, turning it into ^{98}Ru.

Essentially we’re just assuming that if beta decay or electron capture can happen, it will happen. The isotope with the higher energy state will inevitably decay into the other. The reverse won’t happen due to conservation of energy, so it’ll either be stable then or decay into some third element.

Most elements, even if the nucleus isn’t a particularly stable one, will at least get lucky with one or two isotopes. For instance, going two down to Niobium, it has only a single stable isotope ^{93}Nb. So even though it has an odd number of protons, and that’s not great for stability, it still managed to hold onto one (note that this implies ^{93}Mo is unstable–which it is).

So it’s not hard to imagine that out of nearly a hundred natural elements, there would be a couple that lose every battle with both of their neighbors on the stability front. Of course there are good technical reasons why one isobar would be more stable than another, but the details are so involved that it ends up looking a lot like a coin flip in some cases.