OTTAWA — On a night when Canada chased a goal for 90 minutes, substitute Janine Beckie delivered deep into injury time Tuesday to beat Brazil.

Diana Matheson sent a high ball towards the Brazilian goal and Beckie, from outside the penalty box, lobbed substitute goalkeeper Luciana, who had just come on as a late injury replacement. It was virtually the last kick of the game and produced a dramatic 1-0 win before 23,568 fans at TD Place.

"Tonight we showed a bit of grit to stay in the game and the resilience to go and win it," said Canadian coach John Herdman.

"We found that next gear and that's something we'll keep reminding ourselves when we hit the Olympics," he added.

It was Canada's last match on home soil before Rio and Herdman took satisfaction "this close to an Olympics that when it's tight, that we can find a way to win."

Unlike its nervy start in Saturday's 2-0 loss to the eighth-ranked South Americans before 28,604 at Toronto's BMO Field, Canada was composed from the get-go Tuesday and had the better of the play against a much-changed Brazilian starting 11.

The 10th-ranked Canadian women will return to their clubs for the rest of the month before reassembling July 1 for a camp in Vancouver. They head to France in mid-July for a closed-door game against China and then a friendly with No. 3 France in Auxerre on July 23.

The team will leave France for Brazil, where it opens Aug. 3 against No. 5 Australia. Canada will also face No. 2 Germany and No. 95 Zimbabwe in round-robin play.

The Canadians had been after the goal all game. They pressed the action and had more and better scoring chances. As farewells go, it was a bright one with a hint of things to come.

"I think Canada was better than Brazil tonight," Brazilian coach Vadao said through an interpreter. "They made fewer mistakes, had more opportunities and they scored."

Herdman wanted goals and while his team came close on numerous occasions — especially in the final stretch when it was one-way traffic — it took 90-plus minutes to put the Brazilians away.

"It was a mantra with the group — whatever it takes, let's keep pushing until that goal comes and they did," he said.

Canada improved to 6-7-6 all-time against the Brazilians.

Jessie Fleming and Sophie Schmidt pulled the strings in midfield as Canada found the holes in Brazil's defence. Matheson was opportunistic on offence.

Brazil was restricted to counter-attacks. And Canadian 'keeper Stephanie Labbe took charge when the visitors did threaten. At the other end, goalkeeper Barbara saved Brazil on several occasions, often playing like a sweeper to snuff out attacks.

It was the first game in Ottawa for the Canadian women since July 2003, when they defeated Brazil 2-1 with current team members Matheson and Rhian Wilkinson in the starting lineup. Current captain Christine Sinclair was in the squad but sidelined by mononucleosis.

Wilkinson — earning her 175th cap — was in the starting lineup, one of two changes from Toronto. Fleming also came in, with Allysha Chapman and Beckie dropping to the bench. Matheson moved to forward from midfield.

Brazil rang in the changes with six new faces in its starting 11. Star forward Marta, the five time FIFA women's player of the year who scored both goals Saturday, and veteran Formiga started on the bench. Cristiane and Erika, who both play for Paris Saint-Germain, were among the additions.

Kadeisha Buchanan came close for Canada in the 18th minute when her deflection went high after a free kick pinged around the penalty box.

The Canadians pushed the pace with left fullback Ashley Lawrence, named Canada's player of the game Saturday, leading the way as rain fell intermittently despite the evening sunshine.

A desperate tackle by Bruna saved a goal, denying Matheson what seemed like a sure tap-in in the 24th minute after Barbara stopped a Deanne Rose shot.

Brazil's best chance of the half came in the 40th minute when a probing ball found Darlene behind the Canadian defence but her shot was off-target. Andressa Alves also misfired early in the second half.

Barbara earned a yellow card in the 55th minute for decking an onrushing Matheson after a fine feed from Sinclair. The Brazilian' keeper made a good save off Sinclair on the ensuing free kick.

A sliding Barbara saved Brazil again in the 70th when she beat Sinclair to the ball. Brazil, in search of some offence, then brought on Marta. Beckie was stopped by Barbara and then shot high with the clock winding down.

An injured Barbara had to be substituted in injury time. She had signalled to the bench that she had leg cramps with 10 minutes remaining and could not continue in injury time.

Canadian fullback Josee Belanger came on in the second half to earn her 50th cap.

Follow @NeilMDavidson on Twitter