We study any four-dimensional Riemannian manifold (M, g) with harmonic curvature which admits a smooth nonzero solution f to the equation del df =f (Rc - R/n-1 g) + x Rc + y (R)g, where Rc is the Ricci tensor of g, x is a constant and y(R) a function of the scalar curvature R. We show that a neighborhood of any point in some open dense subset of M is locally isometric to one of the following five types: (i) S-2(R/6) x S-2 (R/3) with R > 0, (ii) H-2 (R/6) x H-2(R/3) with R < 0, where S-2(k) and H-2(k) are the two-dimensional Riemannian manifolds with constant sectional curvatures k > 0 and k < 0, respectively, (iii) the static spaces we describe in Example 3, (iv) conformally flat static spaces described by Kobayashi (1982), and (v) a Ricci flat metric. We then get a number of corollaries, including the classification of the following four-dimensional spaces with harmonic curvature: static spaces, Miao-Tam critical metrics and V-static spaces. For the proof we use some Codazzi-tensor properties of the Ricci tensor and analyze the equation displayed above depending on the various cases of multiplicity of the Ricci-eigenvalues.