Turkish authorities have detained President Recep Tayyip Erdogan's key rival in what the main opposition party condemned as a coup attempt.