Sketch of an alternative proof.
First, recall (or see for the first time) the following fact:
Given a continuous function $h: \mathbb{R} \rightarrow \mathbb{R}$, the set $K_h := \{x \in \mathbb{R}: h(x) = 0\}$ is closed.
"Recalling" this fact might seem just like sweeping details under the rug; indeed, it is Exercise $4.3.7$ in Stephen Abbott's introductory textbook Understanding Analysis.
Nevertheless, one can then proceed as follows:
Let $f, g: \mathbb{R} \rightarrow \mathbb{R}$ be continuous, real-valued functions that agree on $\mathbb{Q}$.
Furthermore, let $h = f - g$. Then $h$ is the difference of continuous functions, hence continuous itself; we can now use the fact above to conclude that $\mathbb{Q} \subset K_h \subset \mathbb{R}$.
In particular, $K_h$ is a closed set of real numbers that contains $\mathbb{Q}$.
We are given that the rationals are dense in the reals, i.e., $cl(\mathbb{Q}) = \mathbb{R}$.
Therefore, $K_h = \mathbb{R}$, which means that for all $x \in \mathbb{R}$, we have $h(x) = 0$.
By the definition of $h$, this means for all $x \in \mathbb{R}$ we have $f(x) - g(x) = 0$, i.e., $f(x) = g(x)$.
This "proves" the desired result. QED
N.B. The problem posed here is also in Abbott's text: it is Exercise $4.3.8(b)$.