Maximal solution of a differential equation belongs to manifold $M$. I'm trying to prove the following:

Let $U\subseteq\mathbb R^n$ be an open set and $g\colon U\to\mathbb R^n$ be a (continuous) vector field.
  Suppose that for every $p\in U$ the equation
  $$x' = g(x), \quad x\in U,$$
  admits a unique maximal solution $x(\cdot)$ satisfying $x(0)=p$.
  Now, let $M\subseteq U$ be a (at least) $C^1$ manifold closed in $U$.
  If $g$ is tangent to $M$, that is, if $g(x) \in T_xM$ for every $x\in M$, then every maximal solution of $x' = g(x)$ that intercepts $M$ "lives" entirely in $M$.

I have no clue on how to attack this problem. The first thing I've thought is to use that $M$ is locally a level set of a $C^1$ function $f\colon U\to\mathbb R^m$, say $f^{-1}(0)$, but working "locally" is not compatible with "maximal solution", so I'm stuck.
I really don't know how to proceed.
Any help would be appreciated.
Thanks in advance.
 A: Let's follow your idea, because "working locally" can be, in a sense, compatible with "maximal solution" due to connectivity of the interval.
Explicitly, it suffices to prove that (given a maximal solution $c$ of the differential equation):


*

*If $c(t_0) \in M$, then $c(t)$ belongs to $M$ for all $t$ in a neighbourhood of $t_0$.

*The set of $t$'s such that $c(t)$ belongs to $M$ is closed.


Since $M$ is a closed manifold, the second bullet point is obvious (the set of such $t$'s is $c^{-1}(M)$). The first bullet point amounts precisely to working locally, and showing both shows that $c$ belongs to $M$ in the whole interval (!) of its definition if it has any point at all that is in $M$, since intervals are connected.
To prove the first bullet, let $t_0$ be such that $c(t_0) \in M$. Take a neighbourhood $U$ of $c(t_0)$ such that $M \cap U \simeq W_1 \times \{0\}$, where $W_1 \subset \mathbb{R}^{\dim(M)} $ (local form of immersions). Under this diffeomorphism, the vector field $g$ becomes horizontal on $W_1$. In particular, you can consider its restriction to $W_1 \subset \mathbb{R}^{\dim(M)}$, which gives a flow there. By uniqueness of the trajectories, we then have that the flow of points  in $W_1 \times \{0\}$ must belong there. Therefore, the flow of points in $M \cap U$ must belong there.
A: First of all recall that if $\varphi_t(p)$ denotes the flow through $p$ then
$$\varphi_{t+s}(p) = \varphi_t(\varphi_s(p)).$$
Suppose that there exists $p \in U$ and let $t^*$ be the first time such that $\varphi_t(p) \in M$ for $t < t^*$ and $\varphi_t(p) \in U - M$ for $t > t^*$. We can take such $t^*$ such that $\varphi_{t^*}(p)$ lies on the frontier of $M$ on $U$. Now take $t'> t^*$. Then
$$\varphi_t'(p) = \varphi_{t'- t^* + t^*}(p) = \varphi_{t*}(\varphi_{t'- t^*}(p)).$$ Therefore, since $\varphi_{t^*}(p)$ lies on the frontier of $M$ on $U$ there is a sequence $\varphi_{t_n}(p)$ on $M$ such that 
$$d(M,\varphi_{t'}(p)) = \lim_n d(M,\varphi_{t_n}(\varphi_{t'- t_n})).$$ But
$$0 < d(M,\varphi_{t'}(p)) = \lim_n d(M,\varphi_{t_n}(\varphi_{t'- t_n})(p)) = \lim_n 0 = 0$$ since $t'- t_n$ is such that $\varphi_{t_n}(\varphi_{t'-t_n}(p))$ lies on $M$ since $\varphi_{t' -t_n}(p)$ lies on $M$ once $t_n < t$.
We used $M$ is closed to conclude the limit point belongs to $M$. We used that $g(x)$ is tangent to $M$ for all $x$ tangent to $M$ to ensure that the flow is well defined how it is.
