Hiya, I can only talk about the Psychiatric aspect of your question but I’m sure a lot of it can be applicable to prisoners as well, I’ll split my answer into 1933-1939 & 1939-1945, as the context of the first half helps with the second half;
1933-1939:
From the information that I know of, the Nazi's thought of the mentally ill as "Lebensunwertes Leben" or "Life Unworthy of Life" & this idea really goes back to 1920 however Hitler first implemented a form of this through his Eugenics program on 14th July 1934, soon after he came to power. This act was called "Gesetz zur Verhütung erbkranken Nachwuchses" (Or "The Law for the Prevention of Genetically Diseased Offspring", the "Sterilisation Law" or "Compulsory Sterilisation" for short.) Here's a propaganda poster showcasing the act for "compulsory sterilisation". It reads "Wir strehen nicht allein" ("We do not stand alone").
This act made it mandatory for doctors to report their patients to the "Erbgesundheitsgericht" ("Genetic Health Court") if they suspected the patient to suffer from a list of predetermined "hereditary diseases", these included: "Congential Mental Deficiency", Schizophrenia, "Manic-Depressive insanity" (Bipolar disorder), "Hereditary Epilepsy", Huntington's Disease, Blindness & Deafness.
At the Court hearing a judge, medical officer & doctor would be present who decided under no influence of anyone else (but taking into account the whole proceedings) whether it would be "permissible" to sterilise that person. If it was decided that's what would happen then the person did have the option to appeal the decision to the "Higher Genetic Health Court". If the appealed failed then sterilization would be carried out. Also if one wanted to opt for "voluntary sterilization" then they to would have to go through the court.
By 1935, amendments had been made to the act, these amendments were; 1) The right for appeal to be renounced, 2) if the doctors didn't report them then they could incur a fine & 3) The inclusion of sterilization for the "Rhineland Bastards" (This term was even used as early as the Weimar Republic); Children who were of African/Afro-Caribbean & German descent, these children would automatically be deemed unfit & "unworthy of life".
1939-1945:
In 1939, If anyone was a prisoner or mentally ill, then they most likely would’ve been sent to concentration camps. From October ‘39 - August ‘41, a program called “Action T4” was in place as a method of “forced euthanasia” for the “incurably sick” assessed through medical examination by judges & practioners (a bit like before the war), they then would be granted a "Gnadentod" (“ mercy death”) This is what Hitler had to say about the program:
Reich Leader Bouhler and Dr. med. Brandt are charged with the responsibility of enlarging the competence of certain physicians, designated by name, so that patients who, on the basis of human judgment [menschlichem Ermessen], are considered incurable, can be granted mercy death [Gnadentod] after a discerning diagnosis.
This propaganda poster from ‘38 advertises the possible need for something like T4. It was published in the monthly magazine "Neues Volk" (“New People”), it says; "60,000 Reichsmark is what this person suffering from a hereditary defect costs the People's community during his lifetime. Fellow citizen, that is your money too. Read 'A New People', the monthly magazine of the Bureau for Race Politics of the NSDAP." Also as a little tidbit pro-euthanasia films were released, for example "Ich klage an" (“I accuse”)
The Prisoner equivalent to Action-T4 was “Action 14f13” also known as "Sonderbehandlung" or “Special Treatment”. However in 1941, T4 was stopped after rumours of it became public knowledge & people started to oppose it. It then went underground. After T4 ended between ‘41 & ‘44, the psychiatrists used in T4 were sent to the concentration camps to euthanize the prisoners in 14f13.
Hopefully this answers your question :)
Sources & Further Reading:
2)Henry Friedlander’s The Origins of Nazi Genocide: From Euthanasia to the Final Solution
Dieter Kuntz & Susan D. Bachrach’s Deadly Medicine: Creating the Master Race.
Omar Bartov’s The Holocaust Origins, Implementation, Aftermath