Example 4 Find all positive integers $n$ that satisfy the following condition: if $a \in \mathbf{Z}, (a, n)=1$, then $n \mid \left(a^{2}-1\right)$.


【Analysis】If $n$ is odd, take $a=2$, then
$$
n \mid\left(2^{2}-1\right) \text {, }
$$

it is only possible that $n=1$ or 3. Upon verification, $n=1$ or 3 both satisfy the problem's conditions.
First, assume $n$ is even.
If $(3, n)=1$, take $a=3$, then $n \mid\left(3^{2}-1\right)$, the even number $n$ can only be $2, 4, 8$. Upon verification, $n=2,4,8$ all satisfy the problem's conditions.
Next, assume $n$ is even and $3 \mid n$.
If $(5, n)=1$, take $a=5$, then $n \mid\left(5^{2}-1\right)$, $n$ can only add $6, 12, 24$. Upon verification, all satisfy the problem's conditions (if $(a, 6)=1$, then $(a, 2)=1$, and $(a, 3)=1$, thus, $8 \mid\left(a^{2}-1\right)$, and $3 \mid\left(a^{2}-1\right)$, hence $\left.24 \mid\left(a^{2}-1\right)\right)$.
Next, assume
$$
(2, n) \neq 1,(3, n) \neq 1,(5, n) \neq 1 \text {, }
$$

i.e., $2|n, 3| n, 5 \mid n$, thus $30 \mid n$.
If $(7, n)=1$, take $a=7$, then $n \mid\left(7^{2}-1\right)$, i.e., $n \mid 48$, which contradicts $5 \mid n$. Hence $7 \mid n$.

If $(11, n)=1$, take $a=11$, then $n \mid\left(11^{2}-1\right)$, i.e., $n \mid 120$, which contradicts $7 \mid n$. Hence $11 \mid n$.

If $(13, n)=1$, take $a=13$, then $n \mid\left(13^{2}-1\right)$, i.e., $n \mid 168$, which contradicts $11 \mid n$. Hence $13 \mid n$.

Conjecture: Apart from $n=1,2,3,4,6,8,12,24$, no other positive integers satisfy the problem's conditions.

Let $p$ be the smallest prime that is not a factor of $n$ (i.e., $p$ is the smallest prime such that $(p, n)=1$). Clearly, $p$ is an odd prime.
Assume $p$ is an odd prime greater than 7.
Take $a=p$, then $n \mid\left(p^{2}-1\right)$, i.e.,
$n \left\lvert\, 4 \frac{p-1}{2} \cdot \frac{p+1}{2}\right.$.
By Bertrand's postulate, when $k=$ $\frac{p+1}{2}>3$, there exists a prime $q$, such that
$$
k<q<2 k-2=p-1 \text {. }
$$

Therefore, $q|n, q|\left(p^{2}-1\right)$, which is a contradiction.
In conclusion, $n=1,2,3,4,6,8,12,24$.