Yes, $M$ must be compact. In fact, if $M$ is non-compact, it admits a non-surjective self embedding $f:M\rightarrow M$.
When $n=1$, the only non-compact manifold is $\mathbb{R}$, which obviously admits non-surjctivesurjective self-embeddings. So we assume $n=2$.
We will first construct $f$ for the special case where $M = \mathbb{R}^n$.
Let $\lambda:\mathbb{R}\rightarrow [0,1]$ denote a smooth bump function which is identically $1$ near $0$, and also supported near $0$, and let $\mu:\mathbb{R}\rightarrow (-\infty, 0)$ denote any diffeomorphism which is the identity on $(-\infty,-1)$.
Consider the function $f:\mathbb{R}^n\rightarrow \mathbb{R}^n$ given by $$f(x_1,x_2,..., x_n) = \left(\lambda\left(\sum_{i=2}^n x_i^2\right) \mu(x_1) + \left(1-\lambda\left(\sum_{i=2}^n x_i^2\right)\right)x_1, x_2,...,x_n\right).$$$$f(x_1,x_2,\dotsc, x_n) = \left(\lambda\left(\sum_{i=2}^n x_i^2\right) \mu(x_1) + \left(1-\lambda\left(\sum_{i=2}^n x_i^2\right)\right)x_1, x_2,\dotsc,x_n\right).$$ One easily verifies that $f$ is a diffeomorphism onto its image, which is $\mathbb{R}^n$ with a neighborhood of the non-negative $x_1$-axis removed. We note for later that $f$ is the identity as long as $\sum_{i=2}^n x_i^2$ is large enough, or if $x_1\leq -1$.
Now, we promote this to a construction on a general non-compact $n$-dimensional manifold.
To that end, select a curve $\gamma:[0,\infty)\rightarrow M$ with closed image. For example, one could put a complete metric on $M$ and then choose $\gamma$ to be a geodesic ray. Then $\gamma$ has a neighborhood $V$ for which the pair $(V,\gamma([0,\infty)))$ is diffeomorphic to $(\mathbb{R}^n, \{(x,0....,0): x\geq 0\})$$(\mathbb{R}^n, \{(x,0.\dotsc,0): x\geq 0\})$.
Using the $\mathbb{R}^n$ case, we can now self-embed $V$ into itself in a non-surjective fashion. Extending this map to the rest of $M$ using the identity completes the construction.