Evolution from quantum anomalous Hall insulator to heavy-fermion semimetal in magic-angle twisted bilayer graphene