Just a remark: you need much less than a UFD to conclude that $p^2|q^2 \implies
p \mid q$.
Namely, if $A$ is an integrally closed domain then this holds.
(If $q^2 = a p^2$ for some $a,p,q \in A$ with $p \neq 0$,
then $(q/p)^2 = a$, so --- by integral closeness of $A$ --- we see
$\alpha := q/p \in A$, so $q = p \alpha$ and hence $p | q$ in $A$.)
Note that UFD's are integrally closed, and so the case of a UFD follows from
Note also that Martin Brandenburg's example in comments, namely $\mathbb C[x,y,z]/(zx^2 - y^2)$, corresponds to a surface in $\mathbb A^3$ whose
singular locus is an entire curve (the line $x = y = 0$), which is the
basic geometric cause of an affine ring being non-integrally closed. (Integrally
closed implies that the singular locus has codimension at least two.)