Where is wrong with the following "proof"?
$f(x)$ Riemann-integrable $\Rightarrow$ $f(x)$ continuous almost everywhere $\Rightarrow$ $f(x)=g(x)$ almost everywhere, where $g$ is continuous $\Rightarrow$ $\int_a^x f(t)dt = \int_a^x g(t)dt$ as Lebesgue integrals $\Rightarrow$ $\int_a^x f(t)dt = \int_a^x g(t)dt$ as Riemann integrals $\Rightarrow$ $F(x)=\int_a^x g(t)dt$.
By the Second Fundamental Theorem of Calculus, $F'(x)=g(x)=f(x)$ almost everywhere.
I don't see where the error is.
The issue is with the second implication:
For example, consider any function with a jump discontinuity, for example the Heaviside function
$$f(x) = \begin{cases} 0 & x \le 0\\1 & x > 0\end{cases}.$$
Such an $f$ is Reimann integrable (on $[-1, 1]$, say), and continuous almost everywhere. However, if $g(x) = f(x)$ a.e., then in particular, $g(x) = 1$ for almost every $x \in (0, \delta)$ and $g(x) = 0$ for almost every $x$ in $(-\delta, 0)$. Thus, $g$ cannot be continuous at $0$ (take $\epsilon = 1/2$).