As far as we know, Masjids were not locked in the time of the Messenger of Allah (peace be upon him). They were not furnished, and people were too pious to commit any offenses in them. Later, the Masjids were furnished and included items in them which may be stolen. As people have now become corrupt, it is permissible for the ruler to lock them if he thinks this will be for the good of the public in order to protect the Masjids and the valuable items in them from the corrupt people.May Allah grant us success. May peace and blessings be upon our Prophet Muhammad, his family, and Companions.