The result you mentioned is called algebraic Hartog's lemma. Its proof is long and involves many heavy tools from commutative algebra. Here I give a short outline in which I assume you are familiar with the notion of primary decomposition. For detailed proofs, you can find in books that I cite in below. It will take some efforts to finish all of them but believe me they will do you a favor in future.
The theorem is a consequence of the following theorem
Theorem $1$. Let $A$ be a Noetherian normal domain. Then we do have
$$A = \bigcap_{\mathrm{ht}\mathfrak{p}=1} A_{\mathfrak{p}}$$
in which the intersection is taken in the field of fraction $K(A)$.
Its proof is based on series of fundamental theorems. From now on, let assume all the rings are commutative ring with unity.
Lemma $2$. (determinant trick) Let $M$ be a finitely-generated $A$-module and $\alpha$ an ideal of $A$. Let $\phi$ be an $A$-endormorphism of $A$ such that $\phi(M) \subset \alpha M$. Then $\phi$ satisfies an equation of form
$$\phi^n + a_1 \phi^{n-1} + ... + a_{n-1} = 0, a_i \in \alpha$$
Proof: [M.Atiyah & I.G.McDonald, Introduction to Commutative Algebra, chapter $2$, p.$21$]
Theorem $3$. Let $\alpha$ be a decomposable ideal and $\alpha = \bigcap_{i=1}^n \mathfrak{q_i}$ be a minimal primary decomposition of $\alpha$. Then $\sqrt{\mathfrak{q_i}}$ are precisely prime ideals which occur in the set of ideal $\sqrt{(\alpha:x)} (x \in A)$.
Proof. [M.Atiyah & I.G.McDonald, Introduction to Commutative Algebra, chapter $4$, p.$52$]
About notation, $(\alpha:x)=\left \{y \in A \mid xy \in \alpha \right \}$.
Prime ideals $\sqrt{q_i}$'s are called the prime ideals belong to $\alpha$ or prime divisors of $\alpha$. In case the ring $A$ is Noetherian, we have a stronger result.
Proposition $4$. Let $\alpha \neq (1)$ be an ideal in a Noetherian ring $A$. The the prime ideals which belong a $\alpha$ are precisely the prime ideals which occur in the set of ideals $(\alpha:x), (x \in A)$.
Proof. [M.Atiyah & I.G.McDonald, Introduction to Commutative Algebra, chapter $7$, p.$83$]
The above results are implicitly used in theorems below.
Now let assume $A$ is integral and $K(A)$ its field of fraction. A $A$-submodule $I$ of $K(A)$ is said to be a fractional ideal if $I \neq 0$ and there exists $a \in A$ such that $aI \subset A$. Denote the set $\left \{a \in A \mid aI \subset A \right \}$ then we say $I$ is invertible if $II^{-1}=R$.
The following proposition gives us another way to look at invertible ideals. Most of proofs below can be found in [Matsumura, Commutative Ring Theory] but I think it is worth writing down everything here to help you figure out what is going on.
Proposition $5$. Let $A$ be an integral domain and $I$ a fractional ideal of $A$. Then the following conditions are equivalent:
$I$ is invertible.
$I$ is finitely generated and for every prime ideal $\mathfrak{p}$ of $A$, the fractional ideal $I_{\mathfrak{p}} = IA_{\mathfrak{p}}$ of $A_{\mathfrak{p}}$ is principal.
Proof. $(1) \Rightarrow (2)$ If $II^{-1}=A$ then there exists $(a_i,b_i) \in I \times I^{-1}$ such that $\sum a_i b_i = 1$. Then $(a_i)$'s generate $I$ since for any $x \in I$ we do have $\sum (xb_i)a_i = x$ and $xb_i \in A$. Moreover, at least one of $a_ib_i$ is invertible in $A_{\mathfrak{p}}$ and hence $I_{\mathfrak{p}}=a_i A_{\mathfrak{p}}$.
$(2) \Rightarrow (1)$ We always have $(I^{-1})_{\mathfrak{p}} \subset (I_\mathfrak{p})^{-1}$. If $I$ is finitely generated we shall prove that the equality holds. Let $I = \sum Aa_i$. Let $x \in (I_{\mathfrak{p}})^{-1}$ so $xa_i \in A_{\mathfrak{p}}$ and this implies $xa_i c_i \in A$ for some $c_i \in A - \mathfrak{p}$. Consequently, $cx_ia_i \in A \ \forall i$ for $c = \prod c_i$ and particularly we do have $cx \in I^{-1}$ or $x \in (I^{-1})_{\mathfrak{p}}$. By the hypothesis, $I_{\mathfrak{p}}$ is principal so $I_{\mathfrak{p}}(I_{\mathfrak{p}})^{-1} = A_{\mathfrak{p}}$ $(\bullet)$. Now if $II^{-1} \neq A$ then $II^{-1}$ is contained in a maximal ideal $\mathfrak{m}$, and then $I_{\mathfrak{m}}(I_{\mathfrak{m}})^{-1}=I_{\mathfrak{m}}(I^{-1})_{\mathfrak{m}} \subset \mathfrak{m}A_{\mathfrak{m}}$ which contradicts to $(\bullet)$.
Corollary $6$. Let $A$ be a Noetherian domain and $\mathfrak{p}$ a prime ideal. If $\mathfrak{p}$ is invertible then $\mathfrak{ht}\mathfrak{p}=1$ and $A_{\mathfrak{p}}$ is a discrete valuation ring (DVR). In particular, $A_{\mathfrak{p}}$ is normal because a DVR is a one-dimensional normal Noetherian local ring.
Proof. If $\mathfrak{p}$ is invertible then by $2^{th}$ condition in lemma $5$ we see that $\mathfrak{p}A_{\mathfrak{p}}$ is a principal ideal of $A_{\mathfrak{p}}$. Furthermore, $A_{\mathfrak{p}}$ is Noetherian local ring so $A_{\mathfrak{p}}$ is a DVR. Thus, $\dim A_{\mathfrak{p}}=\mathrm{ht}\mathfrak{p}=1$.
Corollary $7$. Let $A$ be a normal Noetherian local ring then every prime divisor of a principal ideal has height $1$.
Proof. Suppose $a \in A, a \neq 0$ and $\mathfrak{p}$ is a prime divisor of $aA$. By proposition $4$, there exists $b \in A$ such that $(aR:b)=\mathfrak{p}$. Denote $\mathfrak{p}A_{\mathfrak{p}}=\mathfrak{m}$, the unique maximal ideal of $A_{\mathfrak{p}}$ then $(aR_{\mathfrak{p}}:b)=\mathfrak{m}$ so by definition $b/a \in \mathfrak{m}^{-1}$ and $b/a \notin A_{\mathfrak{p}}$. If $(b/a)\mathfrak{m} \subset \mathfrak{m}$ then by using determinant trick we see that $b/a$ is integral over $A_{\mathfrak{p}}$ which is a contradiction with the normality of $A_{\mathfrak{p}}$. Consequently, $(b/a)\mathfrak{m} = A_{\mathfrak{p}}$ and $\mathfrak{m}^{-1}\mathfrak{m}=A$. By previous theorem, $\mathrm{ht}A_{\mathfrak{p}}=\mathfrak{ht}\mathfrak{m}=1$.
Now is the main theorem.
Proof of theorem $1$. We always have $A \subset \cap_{\mathrm{ht}\mathfrak{p}=1}A_{\mathfrak{p}}$. Let take $b/a \in K(A)$ such that $a \neq 0$ and $b \in aA_{\mathfrak{p}}$ for every prime ideal $\mathfrak{p}$ of height $1$. We are going to show that $b/a \in A$ by showing that $J=(aA:b)=A$. It is eay to see that $JA_{\mathfrak{p}} = A_{\mathfrak{p}}$ for each prime ideal $\mathfrak{p}$ of height $1$ so $J$ is not contained in any prime ideal of height $1$. $(\bullet)$
Let take a primary decomposition of $aR$
$$aR = \mathfrak{q}_1 \cap \mathfrak{q}_2 \cap ... \cap \mathfrak{q}_n$$
By corollary $7$, each $\mathfrak{p}_i = \sqrt{\mathfrak{q}_i}$ has height $1$ but by proposition $4$, the set of prime ideals belonging to $J$ is contained in the set of prime ideal belonging to $aR$ - but every such a ideal has height $1$ which contradicts to $(\bullet)$ so $J = A$ or equivalently $b/a \in A$.
The following lemma is much important because it transfers theorem $1$ to a much more geometric theorem, say, your original desired result.
Lemma $8$. Let $X$ be a topological space and $Y \subset X$ is a closed irreducible subset. Let $U \subset X$ be an open set such that $U \cap Y \neq \varnothing$. Then
$$\mathrm{codim}(Y,X) = \mathrm{codim}(Y\cap U,U)$$
Proof. See StackProject.
And finally,
Theorem $9$. (Algebraic Hartog's lemma) Let $X$ be a locally Noetherian normal scheme, and let $U \subset X$ be an open subset with $\mathrm{codim}(X - U) \geq 2$. Then the restriction map $\Gamma(X,\mathcal{O}_X) \to \Gamma(U,\mathcal{O}_X)$ is an isomorphism. In other words: every function $f \in \Gamma(U,\mathcal{O}_X)$ extends uniquely to $X$.
Proof. [U.Gortz and T.Wedhorn, Algebraic Geometry, p.$164$]