If I generate, say, 10000 numbers from the normal distribution (in Matlab) and want to draw a histogram with 10 bins, it resembles the normal distribution pretty accurately. However, if I decide to draw a histogram with 100 bins or 1000 bins, it doesn't look like a normal distribution anymore (it looks more noisy, with some peaks and valleys in different places). It would get better if I had a billion numbers.
Why does it work that way? How to explain it mathematically? My intuition is that as the number of histogram bins gets larger, the probability that the generated number belongs to such a small interval (we have large number of bins) gets smaller. But it's just intuition, not a mathematical explanation.

The Normal distribution is a probability density function. You are correct in your intuition.
This Wikipedia article explains it well - in particular the section entitled "Absolutely continuous univariate distributions"
If the $i$-th bin is $x_i \leq x\leq x_{i+1}$ then the probability, hence the proportion of points, in that bin is
$$Pr[x_i\leq X\leq x_{i+1}]=\int_{x_i}^{x_{i+1}} f_X(x)\mathop{dx}$$
As the size of the bin decreases this gets smaller and you need a larger number of draws overall to achieve a smooth histogram. By "number of draws" I mean the 10000 at the top of your question.
Non-formal argument : each bin is a sample from the parent Normal distribution and will look like the parent distribution as the size of the sample gets larger. For smaller samples (read : larger number of bins relative to number of draws or looking at bins close to the extremes of the histogram) the sample error will be relatively larger and the bin will look less like you expect and the histogram more "noisy" overall.