Inclusion Criteria:  Male or female and age above or equal to 18 years at the time of signing informed consent. Diagnosed with type 2 diabetes mellitus 180 days or more before screening. HbA1c of 7.0-10.0 percentage (53.- 85.8 mmol/mol) (both inclusive) as assessed by central laboratory on the day of screening.  Treated with once daily or twice-daily basal insulin (neutral protamine hagedorn insulin, insulin degludec, insulin detemir, insulin glargine 100 units/mL, or insulin glargine 300 units/mL) 20-80 units/day 90 days or more before screening. Short term bolus insulin treatment for a maximum of 14 days before screening is allowed, as is prior insulin treatment for gestational diabetes. The treatment can be with or without any of the following anti diabetic drugs with stable doses 90 days or more before screening:  Metformin Sulfonylureas(a) Meglitinides (glinides)(a) DPP-4 inhibitors(a) Sodium-glucose co-transporter 2 inhibitors Alpha-glucosidase-inhibitors Thiazolidinediones Marketed oral combination products only including the products listed above.  Body mass index (BMI) less than or equal to 40.0 kg/m^2.  Sulfonylureas, meglitinides (glinides) and DPP-4 inhibitors must be discontinued at randomisation.  Exclusion Criteria:  Female who is pregnant, breast-feeding or intends to become pregnant or is of childbearing potential and not using a highly effective contraceptive method. Anticipated initiation or change in concomitant medication (for more than 14 consecutive days) known to affect weight or glucose metabolism (e.g. treatment with orlistat, thyroid, hormones, or systemic corticosteroids). Treatment with any medication for the indication of diabetes or obesity other than stated in the inclusion criteria within 90 days before screening. Any episodes of diabetic ketoacidosis within 90 days before screening. As declared by the participant or in the medical records. Presence or history of pancreatitis (acute or chronic) within 180 days before screening. Any of the following: Myocardial infarction, stroke, hospitalization for unstable angina pectoris or transient ischaemic attack within 180 days before screening. Chronic heart failure classified as being in New York Heart Association Class IV at screening. Recurrent severe hypoglycaemic episodes within the last year (12 months) as judged by the investigator. Uncontrolled and potentially unstable diabetic retinopathy or maculopathy. Verified by a fundus examination performed within the past 90 days before screening or in the period between screening and randomisation. Pharmacological pupil-dilation is a requirement unless using a digital fundus photography camera specified for non dilated examination.