Demons can say pretty much whatever they want, lying or telling the truth as it suits them.
Jesus had already been accused of consorting with demons by such groups as the Sanhedrin (for example, in Matthew 12:24). He refuted those claims, of course, but they kept coming up, so it's likely that at least some people believed them. In that context, a demon claiming that Jesus was the Messiah would have hurt him much more than it helped. His enemies could not only have handwaved it as a lie, they could claim that Jesus had commanded it to lie "to help its master." So commanding them to tell the truth would have done no good. Even if he hadn't commanded them to tell the truth, they could have said it anyway, precisely because it would have hurt him.
So the truth was no good. But it would have been grossly out of Jesus's character to command the demons to lie, and ultimately, it wouldn't have done any good anyway. If they'd said that Jesus was their master, then his enemies would try to claim that this was, in fact, the truth. If they claimed he was an ordinary person -which would have been obviously untrue to anyone nearby- it would have cast more suspicion on him even without anybody saying anything. There is also, of course, the possibility that they could have lied without being commanded, again because it would have hurt him.
In short, anything a demon said would have been used against Jesus, one way or another. He could not allow them to tell the truth, and he could not allow them to lie, so he took a third option: he didn't let them say anything. It was the only way to avoid getting trapped by their words.