Step 1: For each $x\in N$, there is an open neighborhood $U_x$ of $x$ such that $V_x:=g(U_x)$ is open in $M$ and $g\colon U_x\to V_x$ is a homeomorphism.
Proof: Let $x\in N$, and let $V$ be a neighborhood of $g(x)$ in $M$ such that $f$ is injective on $V$. Let $U_x = g^{-1}(V)$; this is a neighborhood of $x$ in $N$. Let $V_x = f^{-1}(U_x)\cap V$; this is a neighborhood of $g(x)$ in $M$.
If $y\in U_x$, then by the definition of $U_x$ one has $g(y)\in V$. But also $f(g(y)) = y$, so $g(y)\in f^{-1}(U_x)$. This proves $g(y)\in f^{-1}(U_x)\cap V = V_x$, i.e., that $g$ maps $U_x$ into $V_x$. It also proves that $f$ maps $V_x$ surjectively onto $U_x$, since $y = f(g(y))\in f(V_x)$. Putting all of this together, we have derived that $f|_{V_x}\circ g|_{U_x} = Id|_{U_x}$, and, since $f|_{V_x}$ is injective, $g|_{U_x}\circ f|_{V_x} = Id|_{V_x}$.
Step 2: Since $f\circ g = Id$, we must have $g$ is injective. Combining this with step 1 yields that $g$ is a homeomorphism onto an open subset $g(N)$ of $M$.
Step 3: On the other hand, $g\circ f\colon M\to g(N)$ is a retraction of $M$ onto $g(N)$, and thus $g(N)$ is closed.
Step 4: Since $g(N)$ is both open and closed, $g(N) = M$, and hence by step 2, $g$ is a homeomorphism onto $g(N) = M$. The relation $f\circ g = Id$ shows that $f$ is the inverse of $g$, and hence $f$ is also a homeomorphism.