This question doesn’t have a brief answer. Please click on the detailed answer.
Based on the true theory that Shia scholars and ulema believe in, the laws Allah (swt) legislates are all based on the benefits they entail or the harms and disadvantages they prevent, in other words, if something is made wajib, it is because of the one or many important or crucial benefits it entails, and if something is made mustahabb, it is because it entails non-crucial benefits. The same goes for haram and makruh acts; if something is haram, it means that it has one or several extremely bad and harmful outcomes, and if something is makruh, it shows that the bad things it entails aren't very severe or harmful. As for acts that neither have a dominating benefit nor a dominating harm are mubah (which means permissible). One thing that must be noted is that when we say benefit or harm, it has a vast meaning that covers more than just worldly ones; it has to do with all perspectives and existential dimensions of man. 
Keeping this intro in mind, one must say: If exceptional extenuating circumstances come up and one has no choice but to commit a haram act, in this case that dominating harm will cease to exist and a benefit will actually replace it. For instance, if eating from a dead corpse is haram in normal circumstances, it will be so as long as there is no necessity to do so, but if someone gets stuck in a place where the only means of survival is to eat from a dead corpse, it will become wajib to do so because of the necessity  , because protecting one’s life is wajib (and more important)  . The same goes for when the doctor prescribes something that has no substitute, because you have no choice, it will be considered an exception and necessity and will be okay.
We also submitted your question to the offices of the maraje’ and these are the responses we received:
The office of the Grand Ayatullah Khamenei:
There are different cases [and each case’s ruling may differ].
The office of the Grand Ayatullah Makarem Shirazi:
Please specify what your case exactly is so that we can tell you what its ruling is in the case of necessity.
The office of the Grand Ayatullah Sistani:
Your question is very vague, you haven't mentioned what the doctor has asked you to do. In certain cases one is permitted to do things and it won't be a sin, while in other cases it will be a sin and one isn't permitted to do so, your question cannot be answered in its generality.
The office of the Grand Ayatullah Golpaygani:
You will need to explain what the haram act is so that we can give the correct answer, but generally speaking, it isn't true that just because the doctor has prescribed something it will become halal no matter what the circumstances.
The office of Ayatullah Hadavi Tehrani:
Doing haram acts for medical treatment isn't permissible, except in cases that there is no other choice and treatment is restricted to that particular act and if one doesn’t treat the illness or problem, it will lead to serious hardship for the individual. In order to be able to tell what should be done, you can refer to a religious doctor, although in the end it is up to the individual himself/herself.
 According to the fiqhi principle: “ الضرورات تبح المحظورات ”
 Of course, only one case is an exception, and that is when the life of one person depends on him killing another, in this case although there is no choice, nevertheless, one doesn’t have permission from Allah (swt) to do so in order to stay alive.