when it comes to immigration and visa etc. How could he have known that he will not be allowed to leave when for some time back he had been allowed to leave without an exit permit.

The best thing in Qatar is that it all depends upon the person of authority. You cannot challenge his decision or authority.