Background: Juvenile idiopathic arthritis can be refractory to some or all treatment regimens, therefore new medications are needed to treat this population. This trial assessed the efficacy and safety of baricitinib, an oral Janus kinase 1/2-selective inhibitor, versus placebo in patients with juvenile idiopathic arthritis. Methods: This phase 3, randomised, double-blind, placebo-controlled, withdrawal, efficacy, and safety trial was conducted in 75 centres in 20 countries. We enrolled patients (aged 2 to <18 years) with polyarticular juvenile idiopathic arthritis (positive or negative for rheumatoid factor), extended oligoarticular juvenile idiopathic arthritis, enthesitis-related arthritis, or juvenile psoriatic arthritis, and an inadequate response (after ≥12 weeks of treatment) or intolerance to one or more conventional synthetic or biologic disease-modifying antirheumatic drugs (DMARDs). The trial consisted of a 2-week safety and pharmacokinetic period, a 12-week open-label lead-in period (10 weeks for the safety and pharmacokinetic subcohort), and an up to 32-week placebo-controlled double-blind withdrawal period. After age-based dosing was established in the safety and pharmacokinetic period, patients received a once-daily 4 mg adult-equivalent dose of baricitinib (tablets or suspension) in the open-label lead-in period. Patients meeting Juvenile Idiopathic Arthritis-American College of Rheumatology (JIA-ACR) 30 criteria (JIA-ACR30 responders) at the end of the open-label lead-in (week 12) were eligible for random assignment (1:1) to receive placebo or continue receiving baricitinib, and remained in the double-blind withdrawal period until disease flare or up to the end of the double-blind withdrawal period (week 44). Patients and any personnel interacting directly with patients or sites were masked to group assignment. The primary endpoint was time to disease flare during the double-blind withdrawal period and was assessed in the intention-to-treat population of all randomly assigned patients. Safety was assessed in all patients who received at least one dose of baricitinib throughout the three trial periods. For adverse events in the double-blind withdrawal period, exposure-adjusted incidence rates were calculated. The trial was registered on ClinicalTrials.gov, NCT03773978, and is completed. Findings: Between Dec 17, 2018 and March 3, 2021, 220 patients were enrolled and received at least one dose of baricitinib (152 [69%] girls and 68 [31%] boys; median age 14·0 years [IQR 12·0–16·0]). 219 patients received baricitinib in the open-label lead-in period, of whom 163 (74%) had at least a JIA-ACR30 response at week 12 and were randomly assigned to placebo (n=81) or baricitinib (n=82) in the double-blind withdrawal period. Time to disease flare was significantly shorter with placebo versus baricitinib (hazard ratio 0·241 [95% CI 0·128–0·453], p<0·0001). Median time to flare was 27·14 weeks (95% CI 15·29–not estimable) in the placebo group, and not evaluable for patients in the baricitinib group (<50% had a flare event). Six (3%) of 220 patients had serious adverse events during the safety and pharmacokinetic period or open-label lead-in period. In the double-blind withdrawal period, serious adverse events were reported in four (5%) of 82 patients (incidence rate [IR] 9·7 [95% CI 2·7–24·9] per 100 patient-years at risk) in the baricitinib group and three (4%) of 81 (IR 10·2 [2·1–29·7]) in the placebo group. Treatment-emergent infections were reported during the safety and pharmacokinetic or open-label lead-in period in 55 (25%) of 220 patients, and during the double-blind withdrawal period in 31 (38%) of 82 (IR 102·1 [95% CI 69·3–144·9]) in the baricitinib group and 15 (19%) of 81 (IR 59·0 [33·0–97·3]) in the placebo group. Pulmonary embolism was reported as a serious adverse event in one patient (1%; IR 2·4 [95% CI 0·1–13·3]) in the baricitinib group in the double-blind withdrawal period, which was judged to be related to study treatment. Interpretation: Baricitinib was efficacious with an acceptable safety profile in the treatment of polyarticular juvenile idiopathic arthritis, extended oligoarticular juvenile idiopathic arthritis, enthesitis-related arthritis, and juvenile psoriatic arthritis, after inadequate response or intolerance to standard therapy. Funding: Eli Lilly and Company under licence from Incyte.