Kreacher's loyalty shifted towards Harry after Harry treated him with kindness and respect. Harry also gave him the locket that belonged to Regulus Black, Kreacher's beloved former master. This gesture deeply moved Kreacher, leading him to be loyal to Harry during the Battle of Hogwarts.