Let $(X, \mu)$ be your favourite measure space (finite or $\sigma$-finite if you like), let $g \in L^2$ (say, the scalar field of $L^2$ is $\mathbb{R}$, though this probably doesn't matter). Let $\tilde g: X \to \mathbb{R}$ be a measurable function and assume that there exists a norm dense vector subspace $D$ of $L^2$ with the following property:
For every $f \in D$ the function $\tilde g f$ is integrable, and we have $\int \tilde g f \, d\mu = \int g f \, d\mu$.
Question. Does it follow that $\tilde g = g$ almost everywhere?
Remarks.
Under the given assumptions, the propertiy $\tilde g = g$ almost everyhwere is equivalent to $\tilde g \in L^2$. For if $\tilde g \in L^2$, then the integral equality in the assumption extends to all $f \in L^2$ by density, and for $f := \tilde g - g$ we thus obtain that $\lVert \tilde g - g\rVert^2 = \int (\tilde g - g) f \, d\mu = 0$.
In other words, the question asks whether it is impossible to represent, on any dense subspace of $L^2$, a continuous linear functional on $L^2$ by a non-$L^2$-function.
If $D$ is a lattice ideal in $L^2$, meaning that $f_1 \in D$ whenever $\lvert f_1 \rvert \le \lvert f_2 \rvert$ for some $f_2 \in D$, then the answer to the question is yes.
Proof of the claim in the second bullet point. Assume that $D$ is a lattice ideal, and let $f \in D$. There exists a measurable function $s: X \to \mathbb{R}$ of modulus $\lvert s \rvert = 1$ such that $\tilde g f s \ge 0$. Note that this implies $\lvert \tilde g f \rvert = \tilde g f s$. As $\lvert s f \rvert = \lvert f \rvert$ we have $sf \in D$, so it follows that $$ \label{1}\tag{$*$} \int \lvert \tilde g f \rvert \, d\mu = \int \tilde g f s \, d\mu = \int g f s \, d\mu \le \lVert f \rVert \lVert g \rVert. $$ Now, consider a function $0 \le h \in L^2$. By density, there exists a sequence $(f_n)$ in $D$ which converges to $h$. By replacing each $f_n$ with $(h \land f_n) \lor 0$ (where $\land$ denotes the pointwise minimum and $\lor$ denotes the pointwise maximum of functions) we may assume that $0 \le f_n \le h$ for each $n$. Moreover, by then replacing each $f_n$ with the pointwise maximum of the functions $f_1, \dots, f_n$, we may also assume that the sequence $(f_n)$ is increasing. Hence, it follows from the monotone convergence theorem and from \eqref{1} that $$ \int \lvert \tilde g h \rvert \, d\mu = \lim_{n \to \infty} \int \lvert \tilde g \rvert f_n \, d \mu \le \lim_{n \to \infty} \lVert f_n \rVert \lVert g \rVert = \lVert h \rVert \lVert g \rVert. $$ For a general (i.e., not necessarily positive) function $h \in L^2$ we can apply the estimate that we just proved to $\lvert h \rvert$ and thus obtain the same estimate $$ \int \lvert \tilde g h \rvert \, d\mu \le \lVert h \rVert \lVert g \rVert $$ for even all $h \in L^2$. But this shows that $\tilde g\in L^2$, so $\tilde g = g$ almost everyhwere due to the remark in the first bullet point. $\square$