This holds for real analytic functions. Also of course for subclasses such as polynomial functions.
The property that being zero on a nondegenerate interval implies being zero on the connected component$~C$ of the domain containing that interval (which if the domain is $\Bbb R$ means $C=\Bbb R$) is easy to deduce from the definition of analytic function. Suppose $f$ is nonzero somewhere in$~C$ to the right of our interval, let $x_0$ be the infimum of the points where this happens. Then the power series $\sum_{i\geq0}a_i(x-x_0)^i$ giving $f(x)$ in a neighbourhood of$~x_0$ must converge to zero for all sufficiently large $x<x_0$, which forces all coefficients $a_i$ to be zero, and thus $f(x)=0$ in a neighbourhood of$~x_0$ which contradicts the choice of$~x_0$. A similar argument works on the left.
More generally (cited from the linked article), if the set of zeros of an analytic function $f$ has an accumulation point inside its domain, then $f$ is zero everywhere on the connected component containing the accumulation point. The argument is similar, using for $x_0$ the accumulation point.