Can $f(g(x)) = x$ if $g(f(x))$ is not equal to $x$?
There are many examples of this. In such cases, $f$ is called a left inverse of $g$, in contrast to a full (two sided) inverse. To give one example, consider $g \colon [0,\infty) \to \mathbf R$ given by $g(x) = \sqrt x$ and $f \colon \mathbf R \to [0,\infty)$ given by $f(x) = x^2$. We have $$ f\bigl(g(x)\bigr) = \sqrt{x}^2 = x, \qquad x \in [0,\infty) $$ and $$ g\bigl(f(x)\bigr) = |x|, \qquad x \in \mathbf R. $$ So $f \circ g$ is the identity, where $g \circ f$ is not.
Another example is the logarithm, which has branches in the complex plane. Let $$L(x)=\log(x)+ 2 \pi Î$$ then $$\exp(L(x))=x$$ but $$L(\exp(x))=x+2 \pi Î$$