Proof. Assume $f$ is not monotonic. We have points $s_1<t_1$ with $f(s_1) < f(t_1)$ and points $s_2<t_2$ with $f(s_2) > f(t_2)$. Choose $s=\min(s_1,s_2)$ and $t=\max(t_1,t_2)$. The set $f([s,t])$ has a maximum and minimum value. If $f(s)$ and $f(t)$ do not both attain minimum and maximum values of $f$, then the points for which $f$ attains it lie in $(s,t)$, thus $f((s,t))$ fails to be open.
Otherwise, we know that the segment $(s_i, t_i)$ ($1\leq i \leq 2)$ lies in $(s,t)$. Then the same reasoning applies to $(s_i, t)$ or $(s,t_i)$.