We present a new numerical code, X-ECHO, for general relativistic magnetohydrodynamics (GRMHD) in dynamical spacetimes. This aims at studying astrophysical situations where strong gravity and magnetic fields are both supposed to play an important role, such as in the evolution of magnetized neutron stars or in the gravitational collapse of the magnetized rotating cores of massive stars, which is the astrophysical scenario believed to eventually lead to (long) GRB events. The code extends the Eulerian conservative high-order (ECHO) scheme (Del Zanna et al. 2007, A&A, 473, 11) for GRMHD, here coupled to a novel solver of the Einstein equations in the extended conformally flat condition (XCFC). We solve the equations in the 3 + 1 formalism, assuming axisymmetry and adopting spherical coordinates for the conformal background metric. The GRMHD conservation laws are solved by means of shock-capturing methods within a finite-difference discretization, whereas, on the same numerical grid, the Einstein elliptic equations are treated by resorting to spherical harmonics decomposition and are solved, for each harmonic, by inverting band diagonal matrices. As a side product, we built and make available to the community a code to produce GRMHD axisymmetric equilibria for polytropic relativistic stars in the presence of differential rotation and a purely toroidal magnetic field. This uses the same XCFC metric solver of the main code and has been named XNS. Both XNS and the full X-ECHO codes are validated through several tests of astrophysical interest.