The answer is no, and this kind of question is part of the subject of the theory of Borel equivalence relations.
The equivalence relations $\sim$ for which there is a Borel function $g:X\to Z$ into a standard Borel space $Z$, with $x\sim y\iff g(x)=g(y)$ are, by definition, precisely the smooth equivalence relations (see the definition on page 5 of the link above). But there are equivalence relations that are not smooth, such as the relation $E_0$ of eventual equality of infinite binary sequences. You can find the arguments that various relations are not smooth in the article to which I linked (seelinked; see also page 5 of these notes of Simon Thomas and Scott Schneider; my favorite proof of this uses forcing (one adds a Cohen real, and sees where it maps in the extension, and then argues that that image real must be already in the ground model, which is impossible).
The subject of Borel equivalence relations studies the entire hierarchy of Borel equivalence relations under Borel reducibility, which is a kind of complexity notion that in effect analyzes the relative difficulty of classification problems in mathematics, and the smooth equivalence relations occupy a region near the very bottom of the hierarchy, among the simplest relations.