If one had haram money and didn’t know it was haram and bought something, can he keep it after realizing that the money was haram?

They cannot keep it. They have to give it to its rightful owner.