I am asking that is $f(g(x))=x$ a sufficient condition for $g(x)=f^{-1}x$ (but I only want $f(x)$ to be an algebraic function)? At school I am learning about inverse functions and my teacher said that to check if a function $g(x)$ is the inverse function of $f(x)$, I need to check if both $f(g(x))=x$ and $g(f(x))=x$. However, I think that just $f(g(x))=x$ is enough to prove that $g(x)$ is the inverse function $f^{-1}x$. Is it true that I must check for both?
A possible duplicate may be $f(g(x))=x$ implies $f(x)=g^{-1}(x)$, but that person is asking if $f(g(x))=x \implies f(x)=g^{-1}(x) \wedge f^{-1}(x)=g(x)$, not if $f(g(x))=x \implies g(x)=f^{-1}x$.

This is not sufficient. Let $f:\mathbb R\to\mathbb Z$ be $z\mapsto \lceil z\rceil$ and $g:\mathbb Z\to\mathbb R$ be the identity. Clearly, $f(g(z))=z$ for all $z\in\mathbb Z$, but $g(f(z))\neq z$ when $z=\frac12$.
Even if the functions have the same domain and codomain, this still may not be true. Consider @TedShifrin's example from this question, where $f,g:\mathbb R\to\mathbb R$\begin{align*} g(x) &= \dfrac{x}{1+|x|} \\ f(x) &= \begin{cases} \frac{x}{1-|x|}\, & |x|<1 \\ 0 & |x|\ge 1 \end{cases}\,. \end{align*}
Edit: OP asked for an algebraic answer, so here's one: let $f:\mathbb R\to\mathbb R^0=\{x\in\mathbb R\mid x\geq0\}$, and $g:\mathbb R^0\to\mathbb R$ as follows.$$f(x)=x^2$$$$g(x)=\sqrt x$$(where $\sqrt{}$ returns non-negative values)
$$$$