Aims. We present a new numerical code, ECHO, based on a Eulerian conservative high-order scheme for time dependent threedimensional general relativistic magnetohydrodynamics (GRMHD) and magnetodynamics (GRMD). ECHO is aimed at providing a shock-capturing conservative method able to work at an arbitrary level of formal accuracy (for smooth flows), where the other existing GRMHD and GRMD schemes yield an overall second order at most. Moreover, our goal is to present a general framework based on the 3 + 1 Eulerian formalism, allowing for different sets of equations and different algorithms and working in a generic space-time metric, so that ECHO may be easily coupled to any solver for Einstein's equations. Methods. Our finite-difference conservative scheme previously developed for special relativistic hydrodynamics and MHD is extended here to the general relativistic case. Various high-order reconstruction methods are implemented and a two-wave approximate Riemann solver is used. The induction equation is treated by adopting the upwind constrained transport (UCT) procedures, appropriate to preserving the divergence-free condition of the magnetic field in shock-capturing methods. The limiting case of magnetodynamics (also known as force-free degenerate electrodynamics) is implemented by simply replacing the fluid velocity with the electromagnetic drift velocity and by neglecting the contribution of matter to the stress tensor. Results. ECHO is particularly accurate, efficient, versatile, and robust. It has been tested against several astrophysical applications, like magnetized accretion onto black holes and constant angular momentum thick disks threaded by toroidal fields. A novel test of the propagation of large-amplitude, circularly polarized Alfvén waves is proposed, and this allows us to prove the spatial and temporal high-order properties of ECHO very accurately. In particular, we show that reconstruction based on a monotonicity-preserving (MP) filter applied to a fixed 5-point stencil gives highly accurate results for smooth solutions, both in flat and curved metric (up to the nominal fifth order), while at the same time providing sharp profiles in tests involving discontinuities.