The Euclidean algorithm begins with two polynomials $r^{(0)}(x)$ and $r^{(1)}(x)$ such that $\deg r^{(0)}(x) > \deg r^{(1)}(x)$ and then iteratively finds
quotient polynomials $q^{(1)}(x), q^{(2)}(x), \ldots$ and remainder polynomials
$r^{(2)}(x), r^{(3)}(x), \ldots $ of successively smaller degrees
via division
$$\begin{align*}
r^{(0)}(x) &= q^{(1)}(x)\cdot r^{(1)}(x) + r^{(2)}(x)\\
r^{(1)}(x) &= q^{(2)}(x)\cdot r^{(2)}(x) + r^{(3)}(x)\\
\vdots\qquad &= \qquad\qquad\vdots
\end{align*}$$
One version of the Extended Euclidean Algorithm also finds pairs of polynomials
$(s^{(0)}(x),t^{(0)}(x)), (s^{(1)}(x),t^{(1)}(x)),
(s^{(2)}(x),t^{(2)}(x)) \ldots$ where $(s^{(0)}(x),t^{(0)}(x)) = (1,0)$
and $(s^{(1)}(x),t^{(1)}(x)) = (0,1)$ that satisfy the generalized
Bezout identity
$$s^{(i)}(x)\cdot r^{(0)}(x) + t^{(i)}(x)\cdot r^{(1)}(x)
= r^{(i)}(x).$$
These polynomials satisfy the "same" recursion as the remainder polynomials,
viz.,
$$\begin{align*}
r^{(i+1)}(x) &= r^{(i-1)}(x) - q^{(i)}(x)\cdot r^{(i)}(x)\\
s^{(i+1)}(x) &= s^{(i-1)}(x) - q^{(i)}(x)\cdot s^{(i)}(x)\\
t^{(i+1)}(x) &= t^{(i-1)}(x) - q^{(i)}(x)\cdot t^{(i)}(x)\\
\end{align*}$$
This form of the extended Euclidean algorithm is useful in
practical applications since only two polynomials $r, s,$ and
$t$ need to be remembered with each new $(i+1)$-th polynomial
replacing the $(i-1)$-th polynomial which is no longer needed.
In your instance, you have $r^{(0)}(x) = x^3 + 2x+1$ and
$r^{(1)}(x) = x^2 + 1$. You have already computed the quotient
and remainder sequence ending with $r^{(3)}(x) = 2$. Now compute
$t^{(2)}(x)$ and $t^{(3)}(x)$ iteratively using the sequence
of quotient polynomials and write
$$\begin{align*}
s^{(3)}(x)\cdot (x^3 + 2x + 1) + t^{(3)}(x)\cdot(x^2 + 1) &= 2\\
-s^{(3)}(x)\cdot (x^3 + 2x + 1) - t^{(3)}(x)\cdot(x^2 + 1) &= 1\\
(-t^{(3)}(x))\cdot (x^2 + 1) &= 1 ~ \mod (x^3 + 2x + 1)
\end{align*}$$
and deduce that the multiplicative inverse of $x^2 + 1$ in
$\mathbb Z_3[x]/(x^3 + 2x + 1)$ is $-t^{(3)}(x)$. Note that
the $s^{(i)}(x)$ sequence does not need to be computed at all
if all that one needs is the inverse.