It is known observationally that the major axes of galaxy clusters and their brightest cluster galaxies are roughly aligned with each other. To understand the origin of the alignment, we identify 40 cluster-sized dark matter (DM) haloes with masses higher than $5times10^{13}~M_{odot}$ and their central galaxies (CGs) at $zapprox 0$ in the Horizon-AGN cosmological hydrodynamical simulation. We trace the progenitors at 50 different epochs between $0<z<5$. We then fit their shapes and orientations with a triaxial ellipsoid model. While the orientations of both DM haloes and CGs change significantly due to repeated mergers and mass accretions, their relative orientations are well aligned at each epoch even at high redshifts, $z>1$. The alignment becomes tighter with cosmic time; the major axes of the CGs and their host DM haloes at present are aligned on average within $sim 30^{circ}$ in the three dimensional space and $sim 20^{circ}$ in the projected plane. The orientations of the major axes of DM haloes on average follow one of the eigen-vectors of the surrounding tidal field that corresponds to the {it slowest collapsing} (or even stretching) mode, and the alignment with the tidal field also becomes tighter. This implies that the orientations of CGs and DM haloes at the present epoch are largely imprinted in the primordial density field of the Universe, whereas strong dynamical interactions such as mergers are important to explain their mutual alignment at each epoch.