The accuracy of a particular interval when compared to the perfect (just) ratio is measured in cents, with one cents being a logarithmic unit with 1 cent calibrated to 0.01 semitones on a 12-tone equal-tempered scale
$12$ is very good for approximating a variety of different ratios quite accurately, especially the perfect 5th and 4th (the simplest ratios of $\frac{3}{2}$ and $\frac{4}{3}$ are approximated to a very accurate degree-only 2 cents off
One point to be noted, of course, is that in any equal-temperament scale, the perfect fifth and perfect fourth will be equally well approximated. This is proved below
Say that the $a^{th}$ and $b^{th}$ degrees of the scale approximate the ratios $4/3$ and $3/2$ respectively. Then the following represent their distance from just intonation (the amount by which the perfect fourth and fifth intervals are "of tune")
All logarithms are to the base of $2^{1/n}$ where $n$ is the number of intervals
$log(3/2)-a=log(3)-log(2)-a$
$b-log(4/3)=b-2log(2)+log(3)$
Note that the difference between the RHS's above is clearly an integer (as $log(2)$ is an integer)
As the error in semitones is by definition less than $1$ (otherwise a and b would not be the best interval degrees), it is clear that both approximations are off by the same amount
Such intervals such as $4/3$ and $3/2$ are called "complementary intervals".
If we are only concerned with the best way to approximate these two ratios we can use https://oeis.org/A060528 to do so.
From a practical standpoint, for any widely applicable music system, we would want our number of intervals to be in that sequence, as the perfect fifth and fourth are by far the most significant ratios in music.
Furthermore, as there are relatively few ratios which are harmonious to the human ear, a scale with too many intervals would have too many dissonant sounding notes, simply by PHP.
The twelve-tone scale was selected in Europe in the 18th century due to both practical and mathematical factors
Since then, twelve tone equal temperament scale has largely taken precedence all over the world due to the mixing of cultures over the past few hundred years.
So, in conclusion, no, 12 is not the only number possible. However, due to the consonance-spotting capacity of human ear, it belongs to a very short list of possible numbers that could have worked.
Once selected, it worked well enough, there was never a need to change it.