Edit: here's now a computation-free way to prove the existence for $\mathfrak{sl}_n$ and a few more, and discussion.

First, let me start with a simple observation, for an arbitrary Lie algebra $\mathfrak{g}$ over an arbitrary (associative unital) commutative ring $R$, and $\mathfrak{g}\to U(\mathfrak{g})$ the universal enveloping algebra: we have the equivalence of

(i) *There is a Lie $R$-algebra retraction $U(\mathfrak{g})\to\mathfrak{g}$;*

(ii) *there is a unital associative $R$-algebra $A$ and Lie $R$-algebra homomorphisms $\mathfrak{g}\to (A,[\cdot,\cdot])\to\mathfrak{g}$ composing to $\mathrm{Id}_{\mathfrak{g}}$.*

Indeed, (i)$\Rightarrow$(ii): just take $A=U(\mathfrak{g})$. For the converse, use the universal property to get a $R$-algebra homomorphism $U(\mathfrak{g})\to A$, and composing with $A\to\mathfrak{g}$ yields the desired retraction.

So all we need is to find $A$. Fix a field $K$ and denote by $p\ge 0$ its characteristic. Namely for $\mathfrak{g}=\mathfrak{sl}_n(K)$ when $p$ does not divide $n$, we take $A=M_n(K)$ and the retraction there is given by $\nu(v)=v-\frac1{n}\mathrm{Tr}(v)$. (The lengthy computation of my initial post is actually what this retraction $U(\mathfrak{sl}_2)\to M_2\to \mathfrak{sl}_2$ looks like!)

To obtain more examples, one observes that if $\mathfrak{g}$ is endowed with a Cartan grading then any graded Lie subalgebra also inherits a retraction. Let me explain in the case of a diagonalizable Cartan grading. The assumption is that we have a certain subspace $\mathfrak{g}_0$ (with linear dual denoted $\mathfrak{g}_0^*$) Lie algebra grading $\mathfrak{g}=\bigoplus_{\alpha\in\mathfrak{g}_0^*}\mathfrak{g}_\alpha$, such that $\mathfrak{g}_\alpha=\{x\in\mathfrak{g}:[h,x]=\alpha(h)x,\forall h\in\mathfrak{g}_0\}$ for each $\alpha\in\mathfrak{g}_0^*$. If so, for any (unital associative) algebra $A$ endowed with a Lie algebra homomorphism $i:\mathfrak{g}\to (A,[\cdot,\cdot])$ one can define: $A_\alpha=\{x\in A:i(h)x-xi(h)=\alpha(h)x,\forall h\in\mathfrak{g}_0\}$ for each $\alpha\in\mathfrak{g}_0^*$; this is multiplicative in the sense that $A_{\alpha}A_\beta\subset A_{\alpha+\beta}$ for all $\alpha,\beta$, and hence $\bigoplus_{\alpha\in\mathfrak{g}_0^*}A_\alpha$ is a unital subalgebra: in particular, if $i(\mathfrak{g})$ generates $A$ as a subalgebra (which is the only case of interest here since we consider quotients of the universal enveloping algebra), this is an algebra grading of $A$. The retraction has to be grading-preserving. Hence it maps every graded subalgebra to itself.

This applies to graded subalgebras of $\mathfrak{sl}_n$ (with $n$ not divisible by the characteristic $p$): for instance, the 2-dimensional non-abelian Lie algebra for $p\neq 2$, the 3-dimensional Heisenberg Lie algebra (viewed inside $\mathfrak{sl}_3$, or inside $\mathfrak{sl}_4$ to remove the restriction $p\neq 3$), etc, and all products $\prod_i\mathfrak{sl}_{n_i}(K)$.

This also applies under disguised occurrences of $\mathfrak{sl}_n$: for instance, over $\mathbf{R}$, $\mathfrak{so}_3\simeq \mathfrak{sl}_1(\mathbf{H})$ and we have a similar retraction.

This does not apply to other semisimple Lie algebras (i.e., not of type $A_n$ or products thereof). Indeed, assume for simplicity that $K$ is algebraically closed of characteristic zero and that $\mathfrak{g}$ is simple. Then if $\mathfrak{g}$ has the property that some $A$ as in (ii) exists with $A$ finite-dimensional, then $\mathfrak{g}$ is isomorphic to $\prod_i\mathfrak{sl}_{n_i}(K)$ for some family $(n_i)$. Indeed, since $\mathfrak{g}$ is semisimple, one easily deduces that $A$ is semisimple, so the underlying Lie algebra is isomorphic to a direct product $K^\ell\times\prod\mathfrak{sl}_{m_j}(K)$, and all its semisimple quotients have the required form.

So a test-case would be the 10-dimensional Lie algebra $\mathfrak{sp}_4\simeq\mathfrak{so}_5$. As I just said, $A$ in (ii) should be infinite-dimensional. But possibly just a few computations are enough to show that there is no retraction at all.

The question is also reasonable for $\mathfrak{g}$ nilpotent (say, over an algebraically closed field of characteristic zero. Here (ii'), defined as (ii) but without "unital" is a convenient criterion.
((ii) trivially implies (ii') and (ii') implies (ii) by adding a unit: $A'=A\oplus R$, observing that the projection onto $A$ is a Lie algebra homomorphism.) The question is then rephrased as: when is a Lie algebra retract of a Lie algebra whose law is the commutator bracket of some associative law?

Initial post:

Yes, there's such a retraction when $\mathfrak{g}=\mathfrak{sl}_2$.

Write the basis $(h,x,y)$, $[h,x]=2x$, $[h,y]=-2y$, $[x,y]=h$. Denote the Casimir element $c=(h+1)^2+4yx$. I only assume that the ground field $K$ has characteristic $\neq 2$.

The enveloping algebra $U$ has its usual grading $U=\bigoplus_{n\in 2\mathbf{Z}}$. Here $U_0$ is the unital subalgebra generated by $h$ and $c$: it is commutative, and actually a polynomial algebra $K[h,c]$, freely generated by $h$ and $c$, $U_{2n}=x^nU_0$ and $U_{-2n}=y^nU_0$ for $n\ge 0$. In characteristic zero, one can characterize $U_{2n}$ as the $2n$-eigenspace for the derivation $v\mapsto hv-vh$ of $U$. (In arbitrary characteristic, one has a similar description using a 1-dimensional torus of automorphisms instead, but this does not matter.) It is known that there is no zero divisor in $U$, and in particular the multiplication by $x^n$ or $y^n$ from $U_0$ to $U_{\pm 2n}$ is a linear isomorphism.

Define a linear map $r:U\to\mathfrak{sl}_2(K)$ by

on $U_0$ by $r(P(c,h))=\frac{P(4,1)-P(4,-1)}2h$;

on $U_2$ by $r(xP(c,h))=P(4,-1)x$;

on $U_{-2}$ by $r(yP(c,h))=P(4,1)y$;

on $U_{2n}$, $|2n|\ge 4$, as zero.

It maps each of $x,y,h$ to itself, so it is a linear retraction.

**Theorem.** *This is a Lie algebra homomorphism*.

Before checking it, let me insist that the value 4 in the evaluation at the $c$ variable is absolutely not random. The above retraction actually factors through the quotient $U/(c-4)U$, which therefore retracts onto $\mathfrak{sl}_2$, but this is not the case of other quotients $U/(c-t)U$ for $t\neq 4$. Also notice that $c=4$ corresponds to the 2-dimensional representation...

**Proof** of the theorem. I'll use the formula $[A,BC]=[A,B]C+B[A,C]$, and its consequence $$=[A,C]BD+A[B,C]D-C[D,A]B+CA[B,D].$$
Observe that $r$ vanishes on the 2-sided ideal $(c-4)U$, so that $r$ factors through $V=U/(c-4)U$.

Since $r$ preserves the grading, by linearity, we have to show that it preserves the bracket at every given degree. This is trivial in degree $\notin\{-2,0,2\}$.

**In degree zero**, by linearity (and using the vanishing on $(c-4)U$) we have to check that $$[r(x^\ell h^n),r(y^\ell h^m)]=r([x^\ell h^n,y^\ell h^m])$$
for all $\ell,n,m\ge 0$.

This is clear if $\ell=0$ since both brackets lie in degree 0 where everything commutes. If $\ell\ge 2$, the left-hand term is clearly $0$. If $\ell=1$, the left-hand term is
$$[r(xh^n),r(yh^m)]=[(-1)^nx,y]=(-1)^nh.$$
The right-hand term is the evaluation of $r$ at

$$[x^\ell h^n,y^\ell h^m]=[x^\ell,y^\ell]h^{n+m}+x^\ell[h^n,y^\ell]h^m-y^\ell[h^m,x^\ell]h^n+0.$$
We have $hx^\ell=x^\ell(h+2\ell)$, and hence $h^mx^\ell=x^\ell(h+2\ell)^m$, and thus $[h^m,x^\ell]=x^\ell((h+2\ell)^m-h^m)$. Similarly, $[h^n,y^\ell]=y^\ell((h-2\ell)^n-h^n)$. Hence
$$[x^\ell h^n,y^\ell h^m]=[x^\ell,y^\ell]h^{n+m}+x^\ell y^\ell((h-2\ell)^n-h^n)h^m-y^\ell x^\ell((h+2\ell)^m-h^m)h^n$$
$$=x^\ell y^\ell (h-2\ell)^nh^m-y^\ell x^\ell (h+2\ell)^mh^n.$$

A computation yields
$$4^\ell x^\ell y^\ell=(c-(h-1)^2)(c-(h-3)^2)\dots (c-(h-2\ell+1)^2),$$
and similarly
$$4^\ell y^\ell x^\ell=(c-(h+1)^2)(c-(h+3)^2)\dots (c-(h+2\ell-1)^2).$$
At $(c,h)=(4,1)$, evaluation of the polynomial $4^\ell y^\ell x^\ell$ vanishes (because of the term $(c-(h+1)^2)$, because $\ell\ge 1$, while evaluation of the polynomial $4^\ell x^\ell y^\ell$ vanishes, because of the term $(c-(h-3)^2)$... as soon as $\ell\ge 2$. If $\ell\ge 2$, similarly both vanish at $(4,-1)$, and we have $r([x^\ell h^n,y^\ell h^m])=0$ as required.

Now concentrate on $\ell=1$, and write $4xy=c-(h-1)^2$, $4yx=c-(h+1)^2$. We have
$$4[x h^n,y h^m]=(c-(h-1)^2)(h-2)^nh^m-(c-(h+1)^2) (h+2)^mh^n.$$
Evaluation at both $(c,h)=(4,1)$ yields $4(-1)^n$, and at $(4,-1)$ yields $-4(-1)^n$. Thus $r([xh^n,y h^m])=(-1)^nh=[r(xh^n),r(y h^m)]$.

**Now let us turn to degree $-2$** (degree $2$ is similar). We have to show that
$$[r(x^{\ell} h^n),r(y^{\ell+1} h^m)]=r([x^{\ell} h^n,y^{\ell+1} h^m])$$
for all $\ell,n,m\ge 0$. The left-hand term is $0$ for $\ell\ge 1$, and for $\ell=0$ equals $-(1-(-1)^n)y$.

Let us pass to the right-hand term. We have
$$[x^\ell h^n,y^{\ell+1}h^m]=[x^\ell,y^{\ell+1}]h^{n+m}+x^\ell[h^n,y^{\ell+1}]h^m-y^{\ell+1}[h^m,x^\ell]h^n+0$$
$$=[x^\ell,y^{\ell+1}]h^{n+m}+x^\ell y^{\ell+1}((h-2\ell-2)^n-h^n)h^m-y^{\ell+1}x^\ell((h+2\ell)^m-h^m)h^n$$
$$=x^\ell y^{\ell+1}(h-2\ell-2)^nh^m-y^{\ell+1}x^\ell(h+2\ell)^mh^n.$$

We need to write everything as $yP(c,h)$. Anticipating on the result, we define $w_\ell=(c-(h+3)^2)(c-(h+5)^2)\dots (c-(h-2\ell+1)^2)$ and $w'_\ell=(c-(h-5)^2)(c-(h-7)^2)\dots (c-(h-2\ell-1)^2)$, when $\ell\ge 1$. Both are products of $\ell-1$ terms.
Using that $P(c,h)y=yP(c,h-2)$ for every polynomial $P$, one has
$$(x^\ell y^\ell)y=(c-(h-1)^2)(c-(h-3)^2)\dots (c-(h-2\ell+1)^2)y$$
$$=y(c-(h-3)^2)(c-(h-5)^2)\dots (c-(h-2\ell-1)^2),$$
which for $\ell\ge 1$ equals $$yw'_\ell (c-(h-3)^2);$$
for $\ell=0$ this is $y$. Also, one writes $$y^{\ell+1}x^\ell=yw_\ell(c-(h+1)^2).$$
Then, for $\ell\ge 1$, one gets
$$[x^\ell h^n,y^{\ell+1}h^m]=y\Big(w'_\ell (c-(h-3)^2)(h-2\ell-2)^nh^m+w_\ell(c-(h+1)^2)(h+2\ell)^mh^n\Big).$$
Then, because of the factors $(c-(h-3)^2)$ and $(c-(h+1)^2)$, evaluation at $(c,h)=(4,1)$ yields zero. Hence $r([x^\ell h^n,y^{\ell+1}h^m])=0$ for all $\ell\ge 1$. It remains $\ell=0$.

$$[h^n,yh^m]
=y\big((h-2)^n-h^n\big)h^m.$$
Then $r([h^n,yh^m])=((-1)^n-1)y$. This is the desired value.

(Note that by restriction, we also obtain a retraction for the 2-dimensional Lie algebra.)