I would like to provide a more abstract, measure-theoretic explanation. Of course this is overkill for most physics applications, where the simpler explanation provided by Haskell Curry works perfectly nice.
Let $(\Omega, \mathcal{F}, \mu)$ be a measure space. We can of course assume that one of the given functions is zero, say $g=0$. We therefore want to find conditions on the integrals of $f\colon \Omega \to \mathbb{R}$ that ensure its vanishing up to a set of measure zero. This last remark is unavoidable as we are really working in a category of functions defined up to "almost-everywhere equivalence", that is, up to sets of measure zero.
The first statement is pretty simple: if all integrals vanish, then the function itself vanishes.
Lemma If $f\colon \Omega \to \mathbb{R}$ is a measurable function such that
$$\int_A f\, d\mu=0,\qquad \forall A \in \mathcal{F}, $$
then $f=0$ almost everywhere.
Proof. Consider the positive part $f^+$ of $f$, defined as $f^+(x)=\max(f(x), 0)$. This is a measurable function. Since
$$\int_{\Omega} f^+\, d\mu= \int_{\{f\ge 0\}} f\, d\mu =0, $$
and since $f^+\ge 0$ almost everywhere, we can conclude that $f^+=0$ almost everywhere. Indeed, we can decompose the set
$$\left\{f^+> 0\right\}=\bigcup_{n=1}^\infty \left\{f^+\ge \frac{1}{n}\right\},$$
and the sets in the right hand side are all null, because
$$
0=\int_{\left\{f\ge \frac{1}{n}\right\}}f\, d\mu=\int_{\left\{f^+\ge \frac{1}{n}\right\}}f^+\, d\mu\ge\frac{1}{n}\left\lvert \left\{f^+\ge \frac{1}{n}\right\}\right\rvert.$$
So the condition $f^+(x)>0$ can only hold in a measure zero set. Since $f^+$ is nonnegative, this means that is must vanish almost everywhere, as claimed. The proof that the negative part $f^-$ vanishes is analogous.
$\square$
Of course there is no need to check the vanishing of integrals over all measurable sets. One can do with substantially smaller families of integrals.
Theorem Suppose that $f\in L^1(\Omega)$ and that $\mathcal{A}\subset \mathcal{F}$ is a family of measurable sets such that
- For every $A\in \mathcal{A}$ it holds that $\int_A f\, d\mu=0$.
- $\Omega \in \mathcal{A}$ and $\mathcal{A}$ is a $\pi$-system.
- The sigma-algebra generated by $\mathcal{A}$ (which we denote by $\sigma(\mathcal{A})$) is the whole of $\mathcal{F}$.
Then $f=0$ almost everywhere.
Proof. Define
$$\mathcal{A}'=\left\{ A'\in \mathcal{F}\ :\ \int_{A'} f\, d\mu=0\right\}.$$
By assumption we have $\mathcal{A}\subset \mathcal{A}'$. So, clearly,
$$\mathcal{F}=\sigma(\mathcal{A})=\sigma(\mathcal{A}').$$
To conclude we need to show that $\mathcal{A}'$ is a sigma-algebra, which implies that $\mathcal{A}'=\mathcal{F}$ and so the desired conclusion by the Lemma above. Since $\mathcal{A}$ is a $\pi$-system, it will be enough to show that $\mathcal{A}'$ is a $\lambda$-system and then apply the $\pi$-$\lambda$ theorem. We proceed to check the $\lambda$-system properties.
By assumption we have $\Omega \in \mathcal{A}'$. Also, it is immediate that if $A'$ and $B'$ are in $\mathcal{A}'$ and $A'\subset B'$, then
$$\int_{B'\setminus A'} f\, d\mu=0, $$
that is, $B'\setminus A'\in \mathcal{A}'$. Lastly, if $A_1'\subset A_2'\subset \ldots $ is an expanding family in $\mathcal{A}'$, then
$$\int_{\bigcup_n A_n} f\, d\mu= \lim_{n \to \infty} \int_{\Omega} f\chi_{A_n}\, d\mu =0, $$
by the dominated convergence theorem, which is applicable because $\lvert f\chi_{A_n}\rvert \le \lvert f \rvert \in L^1(\Omega)$. $\square$
This theorem applies to the case at hand because the family of all open sets satisfies properties 1, 2 and 3 above when $\mathcal{F}$ is the sigma-algebra of Borel sets of $\mathbb{R}^n$. Actually, there are not many more families that satisfy those properties and are of practical use. The family of all rectangles is one, but the family of all balls (for which the theorem is true) is not, because it fails on property 2. Indeed, the intersection of two balls needs not being a ball. I guess that property 2 can be substituted with the following weaker one:
2$_\mathrm{weak}$. $\Omega\in \mathcal{A}$ and for all $A, B\in \mathcal{A}$ there exists $\varnothing \ne C\in \mathcal{A}$ such that $C\subset A\cap B$.