Let $X$ and $Y$ be standard Borel spaces: topological spaces homeomorphic to Borel subsets of complete metric spaces. Given a surjective Borel map $f:X\to Y$, we get an equivalence relation $\sim_f\subseteq X^2$ given by $x\sim_fx'$ iff $f(x) = f(x')$. Since $\sim_f = (f\times f)^{-1}(\Delta_Y)$ where $\Delta_Y$ is the diagonal of $Y$, we obtain that $\sim_f$ is a Borel subset of $X$.

Now, let $\sim$ be any other equivalence on $X$ which is a Borel subset of $X^2$. Does there always exist a Borel space $Z$ and a Borel map $g:X\to Z$ such that $\sim = \sim_g$? Can we take $g$ to be surjective in such case? What conditions on $\sim$ are sufficient to ensure that $g$ can be chosen to be surjective?

If we could define a topological structure on $X/\!_\sim$ which turns it into a Borel space, then a natural projection $\pi:X\to X/\!_\sim$ would be a desired map $g$.

I think that if we endow $X/\!_\sim$ with the $\pi$-quotient topology, then will turn to be an analytic space, but not Borel in general. Is that correct? In such case, perhaps we can can take $Z$ being a one-point compactification of $X/\!_\sim$, though $g = \pi$ would fail to be surjective in such case.

If endowing with the quotient topology only leads to analytic spaces, can we still introduce some different topology on $X/\!_\sim$ so that it becomes a Borel space and $\pi$ is a Borel map?

**Edited:** as Joel pointed out in his answer, the existence of $(g,Z)$ is equivalent to $\sim$ being smooth, that is there exists a Borel reduction from $\sim$ to $\mathrm{id}_Z$.

Kechris in his book "Classical Descriptive Set Theory" provides sufficient conditions for the smoothness in (18.20) such as existence of a Borel selector, or $\sim$ being a closed subset of a Polish space. Here by a selector is meant a map $h:X\to X$ such that $x\sim h(x)$ and $x\sim x'$ implies $h(x) = h(x')$.

The existence $(g,Z)$ with surjective $g$ is a stronger version of smoothness requiring the existence of a surjective Borel reduction. Existence of a Borel selector of $\sim$ implies the existence such a reduction. The book of Kechris, e.g. (12.16), provides sufficient conditions for the existence of a Borel selector. The existence of surjective $g$ does not necessarily imply the existence of a Borel selector (see my comment to the OP).

The procedure via the quotient topology in 1. may not work in some cases as to be analytic, $X/\!_\sim$ needs not only to be a quotient of a Borel space, but also countably separated.