Kobbie Mainoo's late strike earns a thrilling victory after bitter rivals Liverpool had fought back from two goals down.