This is from Hartshorne's Algebraic Geometry:
It's not clear to me how he concludes that $S:=\{a\in X\mid f(a)=g(a)\}$ is dense in $X$.
Regardless of that, considering $S$ closed, it seems to me that density is irrelevant: notice that $X=(X\setminus U)\cup S$ and, because $X$ is irreducible, we get $S=X$. Right?

Any nonempty open subset of an irreducible space is irreducible and dense.
$U\subset X$ is dense, and $(f-g)^{-1}(0)\supset U$ is closed, so $(f-g)^{-1}(0)=X\implies f=g$ on $X$.
It seems what you have written is also correct.