There is no definite answer for this question. Some people may be able to be on a medication for a period of time and then stop it and still see good results, whereas others may need to be on a medication for the rest of their lives.
One difficulty with medications can be something called state dependent learning, wherein you learn how to handle a situation depending on the state that you are in.
If you are taking medications and expose yourself to a certain stressful situation, then you may attribute your success to the medication and not to the fact that you chose to face the situation. If that is the case, then coming off the medication will change the state you are in, therefore leading to an increased chance of relapse into OCD when you face that situation again.
Therefore, when weaning off of medications, it is best to combine the process with some CBT and ERP so that the effects you start to lose from the medications are replaced by the skills you learn in therapy.
By the time the medications are discontinued completely, the skills learned from therapy will be there to replace the former medication benefits.