A generalization of the Hahn-Mazurkiewicz theorem