Dear Student,
There is some lose of energy when two capacitors are connected whose potentials are different because when they are connected then they behave as a single conductor and try to form an equipotential surface. Therefore energy is lost in the form of heat .
​​​​​​Regards.