According to Quranic teachings, complete knowledge of the unseen only belongs to Allah, and it is Him that knows everything about this world. Some of Allah’s special servants are informed of secrets and knowledge and other impalpable things, they see the unseen and hear what cannot be heard through Allah’s teaching and training and based on the spiritual capability and capacity that they express from themselves. This is a gift that Allah grants to the chosen ones, like the prophets and His apostles. This is why we find that the prophets would speak of revelation (which is an issue related to the unseen) or other issues related to the unseen and foretold the events of the future and these events would take place in exactly the form they had explained. Moreover, knowing that after the prophet, the imams are responsible of guiding man in all physical and spiritual fields, it is necessary for them to know about many issues related to the unseen. Therefore, on certain occasions where it is necessary for them to know about the unseen and they ask for it, Allah grants them the required knowledge.
The word “Gheib” literally means anything hidden from one’s senses and understanding, while the word “Shahadah” means anything that is apparent and evident. One thing can be a Gheib issue to one person and a Shahadah issue to another. This depends on one’s spiritual limitations and his knowledge about this world. (A sentence and a half highlighted in yellow). The Quran says in this regard: “Say," [The knowledge of] the Unseen [and miracles] belongs only to Allah [and depends on his command].” 
Knowing that the knowledge of all beings is limited and that of Allah is complete, it is obvious that knowledge of Gheib is exclusively for Allah and no one other than Him possesses this knowledge; because it is only him that utterly encompasses the world and knows of Gheib and Shahadah. In other words, to Allah everything is considered Shahadah. As for other beings, depending on their intellectual and spiritual capacity some things are known and others are hidden from their eyes and their other senses, thus they either know nothing about them or know very little.
Therefore, allocating knowledge of the unseen to Allah (swt) doesn’t mean that others can't learn of some of these secrets as a result of their worthiness and through Allah's (swt) teaching. This is a blessing that Allah (swt) bestows upon certain chosen servants of His, such as the prophets and apostles. This is why we see the prophets speaking of revelation, which is considered of the unseen, or we see them foretelling future incidents that actually do take place at the time they have predicted. 
There are different opinions in regard to the infallible imams having unconditional knowledge of Gheib and other issues in such a way that they knew about all the events of the past and future including the time and place of their martyrdom.
Some have denied the imams’ unlimited knowledge, which would include their knowledge to the place and time of their martyrdom. However, the majority of Shi’a scholars argue relying on the Almighty’s words in the Quran: “Allah will not acquaint you with the Unseen (in order to tell the believers from the hypocrites, because this is against Allah’s custom), but Allah chooses from His apostles whomever He wishes (and informs them of some of the secrets that are needed for the position of leadership)”  and “Knower of the Unseen, He does not disclose His Unseen to anyone, except to an apostle He approves of. Then He dispatches a sentinel before and behind him”  , that the special servants of Allah can be informed of the Gheib on occasions which this knowledge is necessary. This meaning is also indicated in the ahadith of the imams. Imam Sadiq says in this regard: “When an imam wills to know something, Allah will teach him.” 
Therefore, according to these verses and ahadith and knowing that the prophets are responsible for guiding man in all spiritual and physical fields, they must possess a great amount of knowledge so that they can discharge their duty correctly, and the situation is similar to the imams that are the successors of the prophets. Therefore, they too have knowledge of the Gheib in a vast and unconditional form. 
In this regard it is narrated from Imam Sadiq that he stated: “When an Imam wills to know something, Allah will teach him.” 
 Āl-Imrān: 179 “ا کاَنَ اللَّهُ لِیَذَرَ الْمُؤْمِنِینَ عَلىَ مَا أَنتُمْ عَلَیْهِ حَتىَ یَمِیزَ الخْبِیثَ مِنَ الطَّیِّبِ وَ مَا کاَنَ اللَّهُ لِیُطْلِعَکُمْ عَلىَ الْغَیْبِ وَ لَاکِنَّ اللَّهَ یجَتَبىِ مِن رُّسُلِهِ مَن یَشَاءُ فََامِنُواْ بِاللَّهِ وَ رُسُلِهِ وَ إِن تُؤْمِنُواْ وَ تَتَّقُواْ فَلَکُمْ أَجْرٌ عَظِیمٌ”
 Jinn: 26 and 27 “ عَلِمُ الْغَیْبِ فَلَا یُظْهِرُ عَلىَ غَیْبِهِ أَحَدًاإِلَّا مَنِ ارْتَضىَ مِن رَّسُولٍ فَإِنَّهُ یَسْلُکُ مِن بَینِْ یَدَیْهِ وَ مِنْ خَلْفِهِ رَصَدًا ”
 Kuleini, Usul Kāfī, vol. 1, chapter on when the imams wish to know something, they know it, hadith 3. For further information, see: Man and Knowledge of the Unseen, Question 150 (website: 1056).
 However, not always knowing about Gheib is a virtue, and sometimes can be a deficiency. For example, on the night that Imam Ali slept in the Prophets bed, if he knew that he would not be harmed, then such a thing would not be a means of his spiritual growth; because, in that case, everyone would be willing to sleep in his place. In such a situation not knowing about the Gheib is considered a virtue.
 Adopted from: The Imams’ Supernatural Knowledge of their Martyrdoms, Question 579 (website: 631).